# The code can be commented using # # assignment operators x = 5 y <- 6 7 -> z x y z # reading data in data frame object # need to change the directory appropriately fish.df = read.table("c:\\orie476\\fish.dat", header=T) attach(fish.df) # to list objects ls() # to remove a particular object rm(x) # now x was removed ls() # to remove all objects, go to Misc menu and select the appropriate option # creating arrays of data # concatenation x = c(1,2,3,4) y = 6:9 # sequence command z = seq(0,1,.2) x y z # replicate command a = rep(1:3, 3) b = rep(4:6, 1:3) a b x+y log(x+y) # arithmetic operations (componentwise multiplication) x*y # two ways to raise the components of y to the power specified by x y**x y^x # logical values weight = seq(100, 219, 17) weight weight[1] == 100 weight[1] != weight[2] weight < 170 A = matrix(1:9, ncol=3) # note the order in which the entries of A are filled in A matrix(1:9, ncol=3, byrow=T) # use of index (square) brackets A[1:2, 2:3] B = matrix(seq(0,1,.2), ncol=2) # attaching rows/columns to a matrix (rbind/cbind commands, respectively) cbind(A, 11:13) rbind(A, 17:19) # matrix multiplication A%*%B # transposition of vectors/matrices t(x) # computing inverses solve(A) A[1,1] = 17 solve(A) A%*%solve(A) # two ways to compute inner product of x and y sum(x*y) t(x)%*%y # generating an i.i.d. sample of size from the standard normal distribution z = rnorm(200) # various ways to summarize data hist(z) boxplot(z) mean(z) sd(z) # generating an i.i.d. sample of size 50 from the binomial distr with n=100, p = 0.2 w = rbinom(50, 100, .2) stem(w) # basic regression fishFit1 = lm(count~area, fish.df) summary(fishFit1) fishFit2 = lm(count~area, subset(fish.df, area < 10^5)) summary(fishFit2) # extracting coefficients from fits fishFit1$coef fishFit2$coef # plotting commands (scatterplot) plot(area[area<10^5], count[area<10^5]) # adding lines to the plot abline(fishFit1$coef[1], fishFit1$coef[2]) abline(fishFit2$coef[1], fishFit2$coef[2]) # alternatively, use abline(fishFit1) # extracting coefficients (alternative way); residuals and fitted values coefficients(fishFit1) plot(fitted.values(fishFit1), residuals(fishFit1)) predict(fishFit2, data.frame(area=10^(1:5))) # pnorm(quantile); qnorm(prob); pnorm(1.64) qnorm(.95) qnorm(pnorm(1.96)) pnorm(qnorm(.95)) # similar functions are defined for Student's t and Snedecor's F distribution # pf(q, df1, df2); qf(p, df1, df2) # pt(q, df); qt(p, df) cauchyp.normp <- data.frame(quantiles=(1:20)/10, cauchy.p=pt( (1:20)/10, 1), norm.p=pnorm((1:20)/10))