Presentation is loading. Please wait.

Presentation is loading. Please wait.

Using the R software R is an open source comprehensive statistical package, more and more used around the world. R project web site:

Similar presentations


Presentation on theme: "Using the R software R is an open source comprehensive statistical package, more and more used around the world. R project web site:"— Presentation transcript:

1 Using the R software R is an open source comprehensive statistical package, more and more used around the world. R project web site: http://www.r-project.org/http://www.r-project.org/ Find the mirror nearest to you when downloading

2

3 Wise to read this first!

4 Install with PDF Manual included !! Check this box! (All boxes can be checked if you have enough memory space.)

5 Start  (All )Program  R  R 2.9.0

6 R comes with no typical menu selection graphical user interface (GUI) All must be entered at command level (or by writing scripts). Entering data Functions: c, matrix, cbind, data.frame, read.table Help on functions available i R GUI from Help  R functions (text) …

7 Entering data from keyboard Example: We want to enter the vector x = (1, 2) and the matrix To enter something (whatever) we use the assignment operator “ <-” The function c() combines individual values (comma-spaced) to a vector Assigning a vector:

8 Printing the value on screen: Either enter the variable or use the function print() Note that the output begins with [1]. This is the row number, and in this case x is interpreted as a row vector Listing defined objects (vectors, matrices, data frames): Use the function ls() with no arguments

9 What if we just use ls ? The source code of the function ls() is printed on screen

10 Removing objects: Use the function rm() (Enter x again: )

11 Assigning a matrix: Alternative 1: Use the function matrix() a<-matrix( values,nrow= m,ncol= n ) values is a list of values enclosed in c(), i.e. a row vector or an already defined vector. m is the number of rows and n is the number of columns of the matrix. The number of values must be dividable by both m and n. The values are entered column-wise. The identifiers nrow= and ncol= can be omitted Note the double indexing, first number for row and second number for column

12 Identifiers skipped If row and column numbers are “erroneously” specified: Note! There is a result, though, but the fourth value is omitted.

13 Alternative 2: Concatenating (already existing) columns Use the function cbind() …with already existing columns (vectors): Note! The columns will now be indexed by the original column (vector) names

14 Collecting vectors and matrices with the same number of rows in a data frame Use the function data.frame( object 1, object 2, …, object k ) Matrices need to be protected, otherwise each column of a matrix will be identified as a single object in the data frame. Protection is made with the function I()

15 Objcets within a data frame can be called upon using the syntax dataframe $ object

16 Names of objects within a data frame can be called, set or changed by handling the object names()

17 Reading from an external data file Assume we have our data stored on the file demo.dat in directory D:\undv\732A26 xa.1a.2 12 1 21 -1 Set correct working directory in R: Note! Path must be specified with slashes ( / ) which is Unix-language and not backslashes ( \ ) which is DOS-language. To see which is the current working directory:

18 To read from the file, use the function read.table( filename,header= logical_value,sep= separator ) filename is the name of the file enclosed with double quotes ( ” ” ). It can be specified with the whole path if it is not in the current working directory logical_value is set to TRUE if the columns in the file have headers, otherwise it should be set to FALSE (it is set automatically if omitted, but the result may be “unexpected”) separator is set to the separator sign for the columns in the file, (default is ” ” for blank-separated columns)

19 Note! read.table treats every column of the file as an individual column, i.e. it cannot be used to read a matrix directly into the workspace The columns of a stored matrix must be recombined to create the matrix

20 The matrix can be added to the data frame by using cbind()

21 Writing to an external file The function write.table( dataframe, filename,append= logical_value,sep= logical_value, quote= logical_value,row.names= logical_value,col.names= logical_value ) can be used for different formats of the output dataframe is the name of the data frame to be written on file filename is the name of the file to write to logical_value is either TRUE or FALSE If append=FALSE (default) a file will be created and any existing file with that name will be destroyed. If append=TRUE the data frame will be added (vertical concatenation) to an existing file.

22 Examples: Exploring demo1.dat with Notepad (“Anteckningar” in Swedish) Row numbers!

23 Nothing in output will be quoted

24 Tab-separated, but the first header do not correspond vertically with the first column. The first column of the file is the row number.

25 ( append=FALSE is default and can therefore be omitted for new file creation) Row numbers have now been removed and headers correspond vertically with the columns.

26 Note! Multiple lines can be used for a command input. A carriage return before the command is completed opens a new line with the prompt “+” Column names (headers) have been removed.

27 Calculation The ordinary arithmetic operators “ + ”, “ – ”, “ * ” and “ / ” work element-wise

28 For matrix multiplication use “ %*% ”

29 Matrix operators/functions: transpose b=t(a) b = a T inverse b=solve(a) b = a -1 (when needed) QR-factorization qr=qr(a) Additional arguments possible qr.Q(qr)  Q qr.R(qr)  R x=qr.solve(A,b)  Solves A·x = b

30

31 Solving a linear system of equations, regression estimation

32 Regression model

33

34 Alternatively: “reg” becomes an object as output from qr This object has a number of members ( coef, res, fitted )

35 A more comprehensive regression analysis is done with the function lm() (linear model) Use help(”lm”) to learn more about this function

36

37

38 Putting it together in a script Gather command rows in a text file..Give it extension “.r” Call the script file with command source a<-matrix(c(2,1,1,-1),2,2) b<-c(1,2) x=qr.solve(a,b) print(x) Store in d:\undv\732A26\macro.r “#” precedes a comment

39 When exiting R Workspace can be saved for future sessions: save.image(” core.RData”) saves the workspace into file core.RData where core is replaced by a suitable filename base. To restore a saved workspace: load(” core.RData”) To exit from R type q()

40 More programming Regular sequences: Note! ”<-” can be reversed and most often ”<-” can be replaced by ”=”

41 Repeating patterns Note! Identifier needs to be specified ( times or each )

42 Looping and conditioning

43 Conditions must be within parentheses. Normally: Put “else” directly after “}”

44 Equality condition must be given with operator ” == ” Multiple statements following a for, if, else or while must be separated by semicolon ( ; ) runif(1) gives a random U(0,1) number General usage: runif( n, a, b ) n is the number of values, default: a=0, b=1

45 A more complex example: Simulating regression data Script: x1=c(2,3,5,6,9,10,10,12,13,15) # First x-variable x2=c(1,0,0,1,0,1,1,0,1,1) # Second x-variable y<-as.numeric(1:10) # Dimensioning y for (i in (1:10)) { # Computing y using beta1=1.1 and beta2=-4.7 # Random error is N(0,2) y[i]=12+1.1*x1[i]-4.7*x2[i]+rnorm(1,0,2) } Plot(x1,y)# generates a scatter plot y vs. x1 # Estimating the coefficients: x=cbind(rep(1,each=10),x1,x2) b=qr.solve(x,y) print(b) Store in file regress.r

46

47 Suppose we would like to get empirically derived confidence limits for  1, i.e. not using the normal distribution. beta1<-as.numeric(1:500) # Dimensioning array of b1-values x1=c(2,3,5,6,9,10,10,12,13,15) # First x-variable x2=c(1,0,0,1,0,1,1,0,1,1) # Second x-variable y<-as.numeric(1:10) # Dimensioning y for (trial in 1:500) { for (i in (1:10)) { # Computing y using beta1=1.1 and beta2=-4.7 # Random error is N(0,2) y[i]=12+1.1*x1[i]-4.7*x2[i]+rnorm(1,0,2) } # Estimating the coefficients: x=cbind(rep(1,each=10),x1,x2) b=qr.solve(x,y) # Storing b1 in array beta1[trial]=b[2] } Store in file regress2.r

48

49

50 Bootstrapping the estimated 90th percentile of a sample Assume we wish to assess the 90th percentile of a sample from a Poisson distribution. This means that we wish to assess the properties opf the sample percentile as an estimator of the population percentile in terms of bias 95% confidence Simulate a sample of 40 observations from a Po(7)-distribution, show an initial histogram of the sample values. Draw 500 pseudo-samples with replacement from the original sample In each pseudo-sample, compute the sample percentile Collect the pseudo-sample percentiles, translate them by subtracting the original sample percentile and estimate bias and 95% percentile confidence limits

51 Formulae for 90th sample percentile: Let x (1), …, x (n) depict the sample aranged in ascending order, i.e. x (1) is the smallest value and x (n) is the largest value Calculate i = 0.90·n If i is non-integer, let the 90th percentile be x (I + 1) If i is an integer, let the 90th percentile be (x (i) + x (I + 1) )/2 This construction ensures that at most 90% of the sample values are ≤ 90th percentile at most 10% of the sample values are ≥ 90th percentile

52 # R-script for illustrating bootstrapping of the 90th sample percentile n=40 # Sample size b=500 # Number of bootstrap replications pvec<-as.numeric(1:b)# Dimensioning vector of bootstrapped estimates x=rpois(n,7) # Generate 40 independent Po(7)- observations hist(x,main="Histogram from sample data",xlab=NULL) xsort=sort(x) # Sort the data p90index=0.90*n # Calculate decimal order for 90th percentile if (p90index-floor(p90index)>0) { p90=xsort[floor(p90index)+1]} else { # 90th perc. if decimal order is non-integer p90=(xsort[floor(p90index)]+xsort[floor(p90index)+1])/2} # 90th perc. if decimal order is integer

53 # Bootstrapping loop for (i in 1:b) { u=floor(40*runif(40,0,1)+1);# Vector of integers uniformly on {1,2,...,40} xstar=x[u];# Pseudo sample xstarsort=sort(xstar); if (p90index-floor(p90index)>0) { p90star=xstarsort[floor(p90index)+1]} else { # Copying estimation method p90star=(xstarsort[p90index]+xstarsort[p90index+1])/2} pvec[i]=p90star; } pvec_sort=sort(pvec)# Sorting the bootstrapped estimates pvec_trans=pvec_sort-p90# Subtracting original estimate from sorted bootstr. est.

54 # histogram of translated bootstrap estimates readline("Press to show next graph") hist(pvec_trans,main="Histogram of p90star-p90",xlab=NULL) # Finding 2.5th and 97.5th percentiles: L025index=0.025*b U975index=0.975*b if (L025index-floor(L025index)>0) { L025=pvec_trans[floor(L025index)+1] } else { L025=(pvec_trans[L025index]+pvec_trans[L025index+1])/2 } if (U975index-floor(U975index)>0) { U975=pvec_trans[floor(U975index)+1] } else { U975=(pvec_trans[U975index]+pvec_trans[U975index+1])/2 }

55 # Bias estimate: bias=mean(pvec_trans) # 95% percentile confidence interval: lower=p90-U975 upper=p90-L025 output<-data.frame(p90,bias,lower,upper) names(output)<-c("90th perc.","Bias","Lower 95% limit","Upper 95% limit") print(output)

56

57

58 Huge more to find out! Use the PDF manual (read at least the first chapter) Use the help function ( help(” function ” ) or ? function Use Google (search for “R: what you are looking for”)


Download ppt "Using the R software R is an open source comprehensive statistical package, more and more used around the world. R project web site:"

Similar presentations


Ads by Google