Lab 2 R code

Lab 2 R code - Sheet1 Page 2

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
Sheet1 Page 1 # Reads in account data file accountdata<-read.table("",header=T) accountdata names(accountdata) # Attaches data so we can use the variable names attach(accountdata) # Stores the regression model for use later account.mod<-lm(MARKETRATE~ACCOUNTINGRATE) ## Gives model summary summary(account.mod) ## Gives analysis of variance table anova(account.mod) ## Another way for ANOVA account.aov<-aov(MARKETRATE~ACCOUNTINGRATE) summary(account.aov) ## Code for generating predicted values ## and confidence intervals for mean pred.range<-seq(-10,40,length=100) ## predict(. ..) requires model, grid of values for predictions ## and type of interval predict.mean.matrix<-predict(account.mod, data.frame(ACCOUNTINGRATE = pred.range), interval='confidence') predict.mean.matrix[1:3,] # Plots observed values plot(ACCOUNTINGRATE, MARKETRATE, xlim=c(0,40),ylim=c(-10,40)) # Plots fitted line abline(account.mod) # Plots upper and lower lines(pred.range,predict.mean.matrix[,2],lty=2,col=2,lwd=2)
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Sheet1 Page 2 lines(pred.range,predict.mean.matrix[,3],lty=2,col=2,lwd=2) # Calculates prediction intervals<-predict(account.mod,data.frame(ACCOUNTINGRATE = pred.range), interval='prediction') ## Creates lines for prediction intervals lines(pred.range,[,2],lty=3,col=3,lwd=2) lines(pred.range,[,3],lty=3,col=3,lwd=2) legend("topleft", lty=c(1,2,3), col=c(1,2,3), legend= c('Predicted Value','Conf. for Mean', 'Pred. for Value')) #Splits plotting window par(mfrow=c(2,1)) # Standardized Residual plot qqnorm(resid(account.mod)/sqrt(var(resid(account.mod))), main='Standardized Residuals') qqline(resid(account.mod)/sqrt(var(resid(account.mod)))) #Studentized residuals library(MASS) qqnorm(stdres(account.mod), main='Studentized Residuals') qqline(stdres(account.mod)) ## How close? par(mfrow=c(1,1)) plot(resid(account.mod)/sqrt(var(resid(account.mod))), stdres(account.mod))...
View Full Document

This note was uploaded on 01/15/2010 for the course MATH 423 taught by Professor Steele during the Spring '06 term at McGill.

Page1 / 2

Lab 2 R code - Sheet1 Page 2

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online