Exercise 9
Load data
rm(list = ls()) #clean memory
data9 = read.csv("ex9data") # ta dedomena vriskontai mesa sto data frame data9
Define \(X_2=X^2\) and add it to the data.frame with the data
data9$X2 = data9$X^2
Part (i)
fit9 = lm(Y~., data9)
names(fit9)
## [1] "coefficients" "residuals" "effects" "rank"
## [5] "fitted.values" "assign" "qr" "df.residual"
## [9] "xlevels" "call" "terms" "model"
Yhat = fit9$fitted.values
plot(data9$X, data9$Y)
lines(data9$X,Yhat, col=2)
Part (ii)
summary(fit9)
##
## Call:
## lm(formula = Y ~ ., data = data9)
##
## Residuals:
## Min 1Q Median 3Q Max
## -3.03214 -0.58929 0.03393 1.10893 2.10000
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -1.826e+02 1.768e+01 -10.33 2.73e-06 ***
## X 8.983e+00 7.616e-01 11.80 8.91e-07 ***
## X2 -9.107e-02 7.993e-03 -11.39 1.20e-06 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1.727 on 9 degrees of freedom
## Multiple R-squared: 0.9474, Adjusted R-squared: 0.9357
## F-statistic: 81.03 on 2 and 9 DF, p-value: 1.757e-06
Part (iii). Need to put new data in a data frame
newdata9 = data.frame(X=48, X2=48^2)
predict(fit9,newdata9,interval="conf")
## fit lwr upr
## 1 38.78357 37.05092 40.51622
Part (iv)
predict(fit9,newdata9, interval="pred")
## fit lwr upr
## 1 38.78357 34.51055 43.05659
Part (v)
summary(fit9)
##
## Call:
## lm(formula = Y ~ ., data = data9)
##
## Residuals:
## Min 1Q Median 3Q Max
## -3.03214 -0.58929 0.03393 1.10893 2.10000
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -1.826e+02 1.768e+01 -10.33 2.73e-06 ***
## X 8.983e+00 7.616e-01 11.80 8.91e-07 ***
## X2 -9.107e-02 7.993e-03 -11.39 1.20e-06 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1.727 on 9 degrees of freedom
## Multiple R-squared: 0.9474, Adjusted R-squared: 0.9357
## F-statistic: 81.03 on 2 and 9 DF, p-value: 1.757e-06
Exercise 10
rm(list = ls())
data10 = read.csv("ex10data")
Part (i)
fit10 = lm(Y~., data10)
names(fit10)
## [1] "coefficients" "residuals" "effects" "rank"
## [5] "fitted.values" "assign" "qr" "df.residual"
## [9] "xlevels" "call" "terms" "model"
fit10$coefficients
## (Intercept) X1 X2
## 37.650 4.425 4.375
b0hat = fit10$coeff[1]
b0hat
## (Intercept)
## 37.65
b1hat = fit10$coeff[2]
b1hat
## X1
## 4.425
b2hat = fit10$coeff[3]
b2hat
## X2
## 4.375
Part (ii)
summary(fit10)
##
## Call:
## lm(formula = Y ~ ., data = data10)
##
## Residuals:
## Min 1Q Median 3Q Max
## -4.400 -1.762 0.025 1.587 4.200
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 37.6500 2.9961 12.566 1.20e-08 ***
## X1 4.4250 0.3011 14.695 1.78e-09 ***
## X2 4.3750 0.6733 6.498 2.01e-05 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 2.693 on 13 degrees of freedom
## Multiple R-squared: 0.9521, Adjusted R-squared: 0.9447
## F-statistic: 129.1 on 2 and 13 DF, p-value: 2.658e-09
Part (iii)
names(fit10)
## [1] "coefficients" "residuals" "effects" "rank"
## [5] "fitted.values" "assign" "qr" "df.residual"
## [9] "xlevels" "call" "terms" "model"
errors=fit10$res
qqnorm(errors)
qqline(errors)
plot(fit10$fitted.values, errors)
Tests for suitability of linear model
plot(data10$X1, errors)
plot(data10$X2, errors)
For other types of residuals
library(MASS)
standard.errors = stdres(fit10)
student.errors = studres(fit10)