統計學習導論:基於R應用——第三章習題


第三章習題

部分證明題未給出答案

1.

表3.4中,零假設是指三種形式的廣告對TV的銷量沒什么影響。而電視廣告和收音機廣告的P值小說明,原假設是錯的,也就是電視廣告和收音機廣告均對TV的銷量有影響;報紙的P值高,說明原假設成立,也就是報紙廣告對TV的銷量沒啥影響。

 

2.

KNN回歸和KNN近分類都是典型的非參數方法。這兩者的區別在於,前者的輸入和輸出均為定量值;而后者的輸入和輸入和輸出均為定性值。

 

3.

首先,有題目可知下面關系:Y = 50 + 20(gpa) + 0.07(iq) + 35(gender) + 0.01(gpa * iq) - 10 (gpa * gender)

(a) 當IQ和GPA一定的時候,Y的可變量是35*gender-10(gpa*gender).所以當GPA小的時候,無法判斷前面變量的正負號,而當GPA足夠大的時候,該變量一定是負的。所以當GPA足夠大時,男性平均收入高於女性

(b) 直接套公式Y= 50 + 20 * 4 + 0.07 * 110 + 35 + 0.01 (4 * 110) - 10 * 4= 137.1

(c)錯誤。中文版61頁有比較好的解釋,實驗分層原則規定:如果模型中含有交互項,那么即使主效應的系數的p值不顯著,也應該包含在模型中。

 

4.

(a)一般來說,三次回歸的訓練RSS會比線性回歸的訓練RSS小,因為三次回歸會對數據進行貼近訓練集的擬合。

(b)題目中明確說明該數據的實際模型是線性擬合,所以用三次擬合會產生過擬合,而線性擬合有更好的泛化能力,所以線性回歸的測試RSS小。

(c)答案和(a)一樣

(d)由於不知道實際情況,所以無法判斷。

 

8.

Auto = read.table("Auto.data.txt", header = T ,na.strings="?")
Auto = na.omit(Auto)

(a)

attach(Auto)
lm.fit = lm(mpg ~ horsepower)
summary(lm.fit)

i.由summary的結果來看,F-statistic很大而p-value很小,說明兩者是有相關性的。

ii.由書的54頁可知,看擬合效果如何,得看RSE和R-square。書上55頁講的挺清楚,不過目前不知道RSE在這里怎么解釋擬合效果。。囧。。R-square為0.6059,這說明Y的變異中能被X解釋的部分所占比例有60.59%

iii.由擬合出的參數可知,負相關。

iv.predict(lm.fit, data.frame(horsepower=c(98)), interval="confidence")。結果是24.47,置信區間是(23.97, 24.96)

 predict(lm.fit, data.frame(horsepower=c(98)), interval="prediction")。預測區間是(14.81, 34.12)

(b)

plot(horsepower, mpg)
abline(lm.fit)

(c)

par(mfrow=c(2,2))
plot(lm.fit)

 

9.

(a)

Auto = read.table("Auto.data.txt", header = T ,na.strings="?")
Auto = na.omit(Auto)
pairs(Auto)

(b)

cor(subset(Auto, select=-name))

(c)

lm.fit1 = lm(mpg~.-name, data=Auto)
summary(lm.fit1)

i.有。有f-statistic和p-value值可以判斷

ii.由p-value小於0.05可知,displacement, weight, year, and origin這幾個預測變量和響應變量有顯著關系。

iii.車齡變量的系數是0.75,這說明隨着車齡的增加,車子會越來越耗油。

(d)

par(mfrow=c(2,2))
plot(lm.fit1)

plot(predict(lm.fit1), rstudent(lm.fit1))

(e)

lm.fit2 = lm(mpg~cylinders*displacement+displacement*weight)
summary(lm.fit2)

(f)

lm.fit3 = lm(mpg~log(weight)+sqrt(horsepower)+acceleration+I(acceleration^2))
summary(lm.fit3)

par(mfrow=c(2,2))
plot(lm.fit3)

plot(predict(lm.fit3), rstudent(lm.fit3))

lm.fit2<-lm(log(mpg)~cylinders+displacement+horsepower+weight+acceleration+year+origin,data=Auto)
summary(lm.fit2)

par(mfrow=c(2,2)) 
plot(lm.fit2)

plot(predict(lm.fit2),rstudent(lm.fit2))

 

10.

(a)

library(ISLR)
summary(Carseats)

attach(Carseats)
lm.fit = lm(Sales~Price+Urban+US)
summary(lm.fit)

(b)

由summary(lm.fit)的結果的p-value和t-statistic可知,Price和US與Sales有關,Urban和Sales無關

(c)

Sales = 13.04 + -0.05*Price - 0.02*Urban + 1.20*US,其中Urban和US為YES時,值為1,否則為0

(d)

Price and US

(e)

由上面分析可知,Urban與Sales無關,所以我們可以去掉這個變量

lm.fit2 = lm(Sales~Price+US)
summary(lm.fit2)

(f)

(a)中Multiple R-squared:  0.239,  Adjusted R-squared:  0.234,(e)中Multiple R-squared:  0.239,  Adjusted R-squared:  0.235 ,可知兩者擬合度差不多,而(e)稍微好點

(g)

confint(lm.fit2)

(h)

plot(predict(lm.fit2), rstudent(lm.fit2))

通過這個命令得到的圖,我們可知,stuendtize residuals的范圍在-3到3之間,所以沒有離群點

par(mfrow=c(2,2))
plot(lm.fit2)

通過這個命令得到的圖,我們可知,有一些點遠遠超過了其他點,故存在高桿點

 

11.

按照題目要求先生成x和y

set.seed(1)
x = rnorm(100)
y = 2*x + rnorm(100)

(a)

lm.fit = lm(y~x+0)
summary(lm.fit)

由結果可知,p-value接近0可知,原假設不成立

(b)

lm.fit = lm(x~y+0)
summary(lm.fit)

由結果可知,p-value接近0可知,原假設不成立

(c)

這個問題問得讓我都覺得奇怪。。。答案是說明x和y確實是有關系么

(d)

由(a)中結果可知,t-value為18.73.而(sqrt(length(x)-1) * sum(x*y)) / (sqrt(sum(x*x) * sum(y*y) - (sum(x*y))^2))計算結果為18.72593

(e)

我們把t(x,y)換成t(y,x),會得到t(x,y)=t(y,x)

(f)

對比(a)和(b)結果就行

 

12.

(a)

由公式可知,當相等時,滿足題意

(b)

set.seed(1)
x = rnorm(100)
y = 2*x
lm.fit = lm(y~x+0)
lm.fit2 = lm(x~y+0)
summary(lm.fit)
summary(lm.fit2)

(c)

set.seed(1)
x <- rnorm(100)
y <- -sample(x, 100)
lm.fit <- lm(y~x+0)
lm.fit2 <- lm(x~y+0)
summary(lm.fit)
summary(lm.fit2)

 

13.

這個題目比較簡單。。就是寫代碼

(a)~(g)

set.seed(1)
x = rnorm(100)

eps = rnorm(100, 0, sqrt(0.25))

y = -1 + 0.5*x + eps

plot(x, y)

lm.fit = lm(y~x)
summary(lm.fit)

plot(x, y)
abline(lm.fit, lwd=3, col=2)
abline(-1, 0.5, lwd=3, col=3)
legend(-1, legend = c("model fit", "pop. regression"), col=2:3, lwd=3)

lm.fit_sq = lm(y~x+I(x^2))
summary(lm.fit_sq)

(h)~(i)

這個把eps里的方差值改小一點就行了或者改大一點

set.seed(1)
eps1 = rnorm(100, 0, 0.125)
x1 = rnorm(100)
y1 = -1 + 0.5*x1 + eps1
plot(x1, y1)
lm.fit1 = lm(y1~x1)
summary(lm.fit1)

abline(lm.fit1, lwd=3, col=2)
abline(-1, 0.5, lwd=3, col=3)
legend(-1, legend = c("model fit", "pop. regression"), col=2:3, lwd=3)
set.seed(1)
eps2 = rnorm(100, 0, 0.5)
x2 = rnorm(100)
y2 = -1 + 0.5*x2 + eps2
plot(x2, y2)
lm.fit2 = lm(y2~x2)
summary(lm.fit2)

abline(lm.fit2, lwd=3, col=2)
abline(-1, 0.5, lwd=3, col=3)
legend(-1, legend = c("model fit", "pop. regression"), col=2:3, lwd=3)

(j)

confint(lm.fit)
confint(lm.fit1)
confint(lm.fit2)

 

14.

(a)

比較簡單。,其中,

 

(b)~(e)

cor(x1, x2)
plot(x1, x2)

lm.fit = lm(y~x1+x2)
summary(lm.fit)

lm.fit = lm(y~x1)
summary(lm.fit)

lm.fit = lm(y~x2)
summary(lm.fit)

(f)

不矛盾,因為x1和x2有相關性

(g)

計算離群點和高桿點在10題中做過了

 

15.

(a)

一個一個的做線性回歸。。。累感不愛

(b)

lm.all = lm(crim~., data=Boston)
summary(lm.all)

(c)

x = c(coefficients(lm.zn)[2],
      coefficients(lm.indus)[2],
      coefficients(lm.chas)[2],
      coefficients(lm.nox)[2],
      coefficients(lm.rm)[2],
      coefficients(lm.age)[2],
      coefficients(lm.dis)[2],
      coefficients(lm.rad)[2],
      coefficients(lm.tax)[2],
      coefficients(lm.ptratio)[2],
      coefficients(lm.black)[2],
      coefficients(lm.lstat)[2],
      coefficients(lm.medv)[2])
y = coefficients(lm.all)[2:14]
plot(x, y)

(d)

類似下面代碼一個一個的做回歸。。。

lm.zn = lm(crim~poly(zn,3))
summary(lm.zn)

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM