


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



> # Multiple Regression example > # California rain data > > # I am calling the data set "calirain". > # The variables are: number city precip altitude latitude distance. > > ########## > ## > # Reading the data into R: > > my.datafile <- tempfile() > cat(file=my.datafile, "
Signif. codes: 0 '' 0.001 '' 0.01 '' 0.05 '.' 0.1 ' ' 1 Residual standard error: 11.1 on 26 degrees of freedom Multiple R-Squared: 0.6003, Adjusted R-squared: 0. F-statistic: 13.02 on 3 and 26 DF, p-value: 0. > > anova(rain.reg) Analysis of Variance Table Response: precip Df Sum Sq Mean Sq F value Pr(>F) altitude 1 730.7 730.7 5.9329 0.0220220 * latitude 1 2175.3 2175.3 17.6612 0.0002752 *** distance 1 1903.4 1903.4 15.4538 0.0005593 *** Residuals 26 3202.3 123.
Signif. codes: 0 '' 0.001 '' 0.01 '' 0.05 '.' 0.1 ' ' 1 > > #################################################################################** > > # Testing about sets of coefficients in R: >
> # To test in R whether beta_1=beta_3=0, given that X2 is in the model > # we must specify the full model (with all variables included) and > # the reduced model (with ONLY latitude (X2) included) > > rain.full.model <- lm(precip ~ altitude + latitude + distance) > > rain.reduced.model <- lm(precip ~ latitude) > > # This will give us the F-statistic and P-value for this test: > > anova(rain.reduced.model, rain.full.model) Analysis of Variance Table Model 1: precip ~ latitude Model 2: precip ~ altitude + latitude + distance Res.Df RSS Df Sum of Sq F Pr(>F) 1 28 5346. 2 26 3202.3 2 2144.5 8.7057 0.001276 **
Signif. codes: 0 '' 0.001 '' 0.01 '' 0.05 '.' 0.1 ' ' 1 > > > > #################################################################################** > > # INFERENCES ABOUT THE RESPONSE VARIABLE > > # We want to (1) estimate the mean precipitation for cities of altitude 100 feet, > # latitude 40 degrees, and 70 miles from the coast. > # and (2) predict the precipitation of a new city of altitude 100 feet, > # latitude 40 degrees, and 70 miles from the coast. > > > # The predict() function will give us CIs for the mean of Y and PIs for Y, > # for the values of X1, X2, X3 in the data set. > > x.values <- data.frame(cbind(altitude = 100, latitude = 40, distance = 70)) > > # getting the 90% confidence interval for the mean at X1=100, X2=40 and X3=70: > > predict(rain.reg, x.values, interval="confidence", level=0.90) fit lwr upr [1,] 26.09477 19.95278 32. > > # getting the 90% prediction interval for a new response at X1=100, X2=40 and X3=70: > > predict(rain.reg, x.values, interval="prediction", level=0.90) fit lwr upr [1,] 26.09477 6.194312 45. > > #################################################################################** > ### The following code will produce residual plots for this multiple regression *** > > # residual plot: > > plot(fitted(rain.reg), resid(rain.reg)); abline(h=0) >