R Code for HW7 - , "Cultur" ,...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
# R Code for HW7 Sta108, Fall 2007, Utts ### Problem 9.25 Data = read.table ( "~/Documents/School/Sta108utts/APPENC01.txt" ) names ( Data )= c ( "ID" , "Stay" , "Age" , "Risk" , "Cultur" , "Xray" , "Beds" , "MedSchool" , "Region" , "Census" , "Nurse" , "Facility" ) #(b) #Dataset used above (from file "APPENC01.txt") needs to be edited. #1.Consider observations numbered: 57-113 in your dataset dim ( Data ) Data = Data [ 57 : 113 ,] #2.Remove two predictors not needed for the analysis: #these are categorical variables: "MedSchool","Region" (Columns 8,9) #3.remove ID column not needed for the analysis (Column 1) Data = Data [,- c ( 1 , 8 , 9 )] #4.transform the response Y to: log10(Y) Data $ Stay = log10 ( Data $ Stay ) #Now, your dataset is clean to be used in the problem Data DataY = Data [, 1 ] #separate response Y DataX = Data [,- 1 ] #separate predictors, X variables pairs ( DataX ) cor ( DataX ) #(c) (NOTE THAT Y IS NOW THE LOG10 TRANSFORMED VERSION OF THE ORIGINAL Y, FROM #4 ABOVE) library ( leaps ) leaps ( x = DataX , y = DataY , names = c ( "Age" , "Risk"
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: , "Cultur" , "Xray" , "Beds" , "Census" , "Nurse" , "Facility" ), method = "Cp" ) #Three best models are chosen by 3 lowest Cp criterions #To automatically print models in the increasing order of Cp criterion: ModelSel = leaps ( x = DataX , y = DataY , names = c ( "Age" , "Risk" , "Cultur" , "Xray" , "Beds" , "Census" , "Nurse" , "Facility" ), method = "Cp" ) ModelSel $ which [ order ( ModelSel $ Cp ), ] #To print Cp criterion in increasing order sort ( ModelSel $ Cp ) #To plot Cp against p, and add reference line: Cp=p plot ( ModelSel $ size , ModelSel $ Cp , pch = 19 ) abline ( , 1 ) #Fit the best chosen model Fit = lm ( Stay ~ Age + Xray + Census , data = Data ) Fit #Residual plot plot ( Fit $ fitted.values , Fit $ residuals , main = "Residuals vs. Fitted Values" , xlab = "Fitted Values" , ylab = "Residuals" , pch = 19 ) abline ( h = )...
View Full Document

This note was uploaded on 09/01/2011 for the course ISYE 6414 taught by Professor Staff during the Fall '08 term at Georgia Institute of Technology.

Ask a homework question - tutors are online