{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Lab 2 R code

# Lab 2 R code - Sheet1 Page 2...

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

Sheet1 Page 1 # Reads in account data file accountdata<-read.table("account.data.txt",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)

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

View Full Document
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.int.matrix<-predict(account.mod,data.frame(ACCOUNTINGRATE = pred.range), interval='prediction') ## Creates lines for prediction intervals lines(pred.range,predict.int.matrix[,2],lty=3,col=3,lwd=2) lines(pred.range,predict.int.matrix[,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

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern