Norwegian Service Centre for Climate Modelling -> Graphic Tools -> The statistical packgage R
 
 

 

     

The statistical packgage R

A scripting language for statistical analysis is now installed on Gridur. The R package is an open source version of the commercial S Plus. More information, including comprehensive manuals, can be found on:
http://www.R-project.org
To start up R on Gridur, you first have to run the following command when using the C-shell (/bin/csh or /bin/tcsh):
module load R
This command only makes R available for later use; you will not enter the R interactive shell by only submitting this command. After running the command, you may (if you wish) start up another unix shell (for example bash).

To start up R in interactive mode, just type:

R
You may also run an R program in batch using this command:
R --vanilla <r_program >r_output
Below follows an annotated example of an R program, which may be useful for those with no former experience with R:
postscript("r.eps",horizontal=FALSE,onefile=FALSE)
# Arranges for graphical output to be sent to
# a postscript file, r.eps. The 'onefile=FALSE'
# ensures the file will follow the encapsulated
# postscript standard.
par(mfrow=c(2,3))
# Organizes the graphical ouput device using
# 6 plotting areas on one page layed out in
# a 2 columns by 3 rows structure. Each 'plot'
# command below will send its output to a
# new area.
s1 <- read.table("s1",header=TRUE)
# The '<-' is the "assign to" operator. The
# 'read.table' command reads an ASCII file (s1)
# organized in columns separated by spaces.
# The 'header=TRUE' option is used when the
# first row contains names for each coloumn.
# The variable s1 will now hold an object
# representing the table.
plot(s1$words,s1$length)
plot(s1$int,s1$length)
plot(s1$char,s1$length)
plot(s1$long,s1$length)
# Fills the first four plotting areas with
# scatter plots. The 's1$words' and similar
# constructs represents vectors corresponding
# to coloumns in the table. 'words' is the
# column name.
m1.s1 <- lm(length ~ words + int,data=s1)
# Fits a linear regression model using
# 'length' as the dependent variable and
# 'words' and 'int' as independent variables
summary(m1.s1)
# Prints a summary of the regression
plot(residuals(m1.s1),s1$length)
m2.s1 <- lm(log(length) ~ I(log(words)) + I(log(int)),data=s1)
# Trying a logarithmic fit
summary(m2.s1)
plot(residuals(m2.s1),log(s1$length))
q()
# Quit R
The program will read a file "s1" with the following content:
        char    int     long    lines   words   length
        6       41      4       778     2897    17775
        0       15      0       198     842     5094
        7       22      3       1061    3342    24495
        15      24      0       930     4204    34453
        1       104     20      1575    5842    36234
        17      191     14      2175    9074    59492
        6       30      13      677     2879    17820
        6       8       0       109     573     3515
        1       122     13      1038    4291    28360
        2       1       0       53      345     2086
        4       6       0       83      389     2394
        19      29      7       722     2358    17143
        1       18      0       280     1010    7448
        88      566     15      4254    16107   130131
More examples can be found on /noserc2/extdata/felles/R on Gridur, but some of these files depend on libraries not yet installed on Gridur.
 

Send comments to webmaster