## BERD R Short Course ## Session 2 Homework Key # 1. List all R packages installed on your computer library () # 2. Search on line to find an R package that does Classification and Regression Trees (CART). # You may use rpart package # 3. Install the R package your found in Problem 2 to your computer install.packages("rpart") # 4. List all functions in the R package found in Step 2 help(package="rpart") # 5. List all datasets contained in the R package found in Step 2 data (package="rpart") # 6. Run R command help (airquality) to learn more about the data frame airquality contained in the base package help(airquality) # 7. Select Day 2 data from airquality with Temp > 90, and keep only the Temp variable subset (airquality, Day==2 & Temp>90) # 8. Write your own function one.t.test so that it will accept 2 arguments y and alpha, # where y is a numeric vector and alpha is the significance level with a default value of .05. one.t.test=function(y,alpha=.05){ z=y[!is.na(y)] n=length(z) if(n==1) stop ("the number of obs should by > 1") z.bar=mean(z) z.sd=sd(z) T=z.bar/(z.sd/sqrt(n)) p=2*pt(abs(T),n-1,lower.tail=F) LCI=z.bar-qt(1-alpha/2,n-1)*z.sd/sqrt(n) UCI=z.bar+qt(1-alpha/2,n-1)*z.sd/sqrt(n) CI=c(LCI,UCI) list(T=T,p=p,CI=CI,df=n-1,z.bar=z.bar, z.sd=z.sd, alpha=alpha, y=y) } y=1 one.t.test(y) y=rnorm(30) one.t.test(y) t.test(y)