Lab 1 Solutions.pdf - R Lab 1 Assignment > > > >...

This preview shows page 1 - 7 out of 11 pages.

Page 1 of 11 R Lab 1 Assignment > library(MASS) > library(ISLR) > fix(Boston) > names(Boston) [1] "crim" "zn" "indus" "chas" "nox" "rm" "age" "dis" "rad" [10] "tax" "ptratio" "black" "lstat" "medv" > lm.fit=lm(medv~lstat) Error in eval(predvars, data, env) : object 'medv' not found > lm.fit=lm(medv~lstat,data=Boston) > attach(Boston) > lm.fit=lm(medv~lstat) > lm.fit Call: lm(formula = medv ~ lstat) Coefficients: (Intercept) lstat 34.55 -0.95 > summary(lm.fit) Call: lm(formula = medv ~ lstat) Residuals: Min 1Q Median 3Q Max -15.168 -3.990 -1.318 2.034 24.500 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) 34.55384 0.56263 61.41 <2e-16 *** lstat -0.95005 0.03873 -24.53 <2e-16 *** --- Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 1 Residual standard error: 6.216 on 504 degrees of freedom Multiple R-squared: 0.5441, Adjusted R-squared: 0.5432 F-statistic: 601.6 on 1 and 504 DF, p-value: < 2.2e-16 > names(lm.fit) [1] "coefficients" "residuals" "effects" "rank" "fitted.values" [6] "assign" "qr" "df.residual" "xlevels" "call" [11] "terms" "model" > coef(lm.fit) (Intercept) lstat 34.5538409 -0.9500494 > confint(lm.fit) 2.5 % 97.5 % (Intercept) 33.448457 35.6592247 lstat -1.026148 -0.8739505 > predict(lm.fit,data.frame(lstat=(c(5,10,15))), interval="confidence")
Page 2 of 11 fit lwr upr 1 29.80359 29.00741 30.59978 2 25.05335 24.47413 25.63256 3 20.30310 19.73159 20.87461 > predict(lm.fit,data.frame(lstat=(c(5,10,15))), interval="prediction") fit lwr upr 1 29.80359 17.565675 42.04151 2 25.05335 12.827626 37.27907 3 20.30310 8.077742 32.52846 > plot(lstat,medv) > abline(lm.fit) > abline(lm.fit,lwd=3)
Page 3 of 11 > abline(lm.fit,lwd=3,col="red") > plot(lstat,medv,col="red") > plot(lstat,medv,pch=20)
Page 4 of 11 > plot(lstat,medv,pch="+") > plot(1:20,1:20,pch=1:20) > par(mfrow=c(2,2)) > plot(lm.fit)
Page 5 of 11 > plot(predict(lm.fit), residuals(lm.fit)) > plot(predict(lm.fit), rstudent(lm.fit)) > plot(hatvalues(lm.fit))
Page 6 of 11 > which.max(hatvalues(lm.fit)) 375 375 > lm.fit=lm(medv~lstat+age,data=Boston) > summary(lm.fit) Call: lm(formula = medv ~ lstat + age, data = Boston) Residuals: Min 1Q Median 3Q Max -15.981 -3.978 -1.283 1.968 23.158 Coefficients: Estimate Std. Error t value Pr(>|t|)

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture