請讀入「geese.txt」資料。

```r
library(readr)
geese <- read.csv("C:/Users/tony8/Downloads/geese.txt", sep="")
head(geese)
```

```
##   photo obs1 obs2
## 1    56   50   40
## 2    38   25   30
## 3    25   30   40
## 4    48   35   45
## 5    38   25   30
## 6    22   20   20
```
  1. 將photo當成反應變數,obs1為解釋變數,配適迴歸模型。

    m1=lm(photo~obs1,data = geese)
    e=m1$resid
    m1
    ## 
    ## Call:
    ## lm(formula = photo ~ obs1, data = geese)
    ## 
    ## Coefficients:
    ## (Intercept)         obs1  
    ##     26.6496       0.8826
  1. 所配適之模型為何? \(\hat{photo}\)=26.6496 +0.8826\(obs1\)

  2. obs1是否為顯著之解釋變數?

    summary(m1)
    ## 
    ## Call:
    ## lm(formula = photo ~ obs1, data = geese)
    ## 
    ## Residuals:
    ##      Min       1Q   Median       3Q      Max 
    ## -125.928  -18.713   -9.033   11.699  161.711 
    ## 
    ## Coefficients:
    ##             Estimate Std. Error t value Pr(>|t|)    
    ## (Intercept) 26.64957    8.61448   3.094  0.00347 ** 
    ## obs1         0.88256    0.07764  11.367 1.54e-14 ***
    ## ---
    ## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
    ## 
    ## Residual standard error: 44.41 on 43 degrees of freedom
    ## Multiple R-squared:  0.7503, Adjusted R-squared:  0.7445 
    ## F-statistic: 129.2 on 1 and 43 DF,  p-value: 1.537e-14

    \(H_0\):\(\beta\)係數為0
    \(H_1\):\(\beta\)係數不為0
    由於檢定結果之p值小,推翻虛無假設,表示obs1為顯著之解釋變數

  3. 檢定殘差是否具常態性?

    qqnorm(e)
    qqline(e)

    shapiro.test(e)
    ## 
    ##  Shapiro-Wilk normality test
    ## 
    ## data:  e
    ## W = 0.83572, p-value = 1.604e-05

    \(H_0\):殘差具常態性
    \(H_1\):殘差不具常態性
    因為shapiro.test結果之p值小,所以推翻虛無假設,表示殘差不具常態性

  4. 檢定殘差是否具一階自我相關?

    library(lmtest)
    ## Warning: 套件 'lmtest' 是用 R 版本 4.2.3 來建造的
    ## 載入需要的套件:zoo
    ## Warning: 套件 'zoo' 是用 R 版本 4.2.3 來建造的
    ## 
    ## 載入套件:'zoo'
    ## 下列物件被遮斷自 'package:base':
    ## 
    ##     as.Date, as.Date.numeric
    library(car)
    ## 載入需要的套件:carData
    durbinWatsonTest(m1) ##雙尾檢定
    ##  lag Autocorrelation D-W Statistic p-value
    ##    1     -0.05571401      2.107238    0.77
    ##  Alternative hypothesis: rho != 0
    dwtest(m1)           ##單尾檢定
    ## 
    ##  Durbin-Watson test
    ## 
    ## data:  m1
    ## DW = 2.1072, p-value = 0.6066
    ## alternative hypothesis: true autocorrelation is greater than 0

    \(H_0\):殘差無一階自我相關
    \(H_1\):殘差有一階自我相關
    由於兩種檢定結果之p值大,則無法推翻虛無假設,表示殘差無一階自我相關

  5. 做均齊性變異數檢定。

    library(car)
    library(carData)
    ncvTest(m1)
    ## Non-constant Variance Score Test 
    ## Variance formula: ~ fitted.values 
    ## Chisquare = 81.41318, Df = 1, p = < 2.22e-16
    library(lmtest)
    bptest(m1)
    ## 
    ##  studentized Breusch-Pagan test
    ## 
    ## data:  m1
    ## BP = 24.366, df = 1, p-value = 7.964e-07

    \(H0\):變異數具均齊性(變異數為常數)
    \(H1\):變異數不具均齊性
    ncvTest 檢定結果之p值小,推翻虛無假設,表示變異數不具均齊性,所以變異數不為常數
    bptest 檢定結果之p值小,推翻虛無假設,表示變異數不具均齊性,所以變異數不為常數

  1. 將photo做boxcox轉換
  1. 用powerTransform(model)求出\(\lambda\)值 ## model 為 (1) 中所做之模型

    trans=powerTransform(m1)
    summary(trans)
    ## bcPower Transformation to Normality 
    ##    Est Power Rounded Pwr Wald Lwr Bnd Wald Upr Bnd
    ## Y1    0.3747         0.5       0.1074       0.6421
    ## 
    ## Likelihood ratio test that transformation parameter is equal to 0
    ##  (log transformation)
    ##                            LRT df      pval
    ## LR test, lambda = (0) 6.756736  1 0.0093394
    ## 
    ## Likelihood ratio test that no transformation is needed
    ##                            LRT df       pval
    ## LR test, lambda = (1) 23.45594  1 1.2781e-06

    \(\hat{\lambda}\) =0.3747272
    \(\lambda\)=0.3取代\(\hat{\lambda}\)

  2. 用photo2 = bcPower(photo,\(\lambda\))做boxcox轉換

    photo2=bcPower(geese$photo,0.3)
    photo2
    ##  [1]  7.818215  6.593571  5.421759  7.314252  6.593571  5.092358  5.092358
    ##  [8]  6.896146  6.267798  4.023944  5.913970  3.110607  4.600088  5.421759
    ## [15]  8.163977  5.525382  9.437627  7.818215  3.510455  8.381657  6.896146
    ## [22]  5.913970  9.524018 10.647815 12.088071 11.713040 13.125722 16.915301
    ## [29] 15.857179 13.004247  8.741346 10.787173 11.653370  8.590288  9.524018
    ## [36] 10.321819  9.734267  7.877585  6.968613  7.758097 15.565881 10.468927
    ## [43]  9.215466  9.566710  7.818215
  3. 重新配適模型,以photo2為反應變數

    m2=lm(photo2~geese$obs1)
    summary(m2)
    ## 
    ## Call:
    ## lm(formula = photo2 ~ geese$obs1)
    ## 
    ## Residuals:
    ##     Min      1Q  Median      3Q     Max 
    ## -5.4368 -1.2332  0.2836  1.3344  3.2067 
    ## 
    ## Coefficients:
    ##             Estimate Std. Error t value Pr(>|t|)    
    ## (Intercept) 6.402654   0.374217   17.11  < 2e-16 ***
    ## geese$obs1  0.029783   0.003373    8.83 3.27e-11 ***
    ## ---
    ## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
    ## 
    ## Residual standard error: 1.929 on 43 degrees of freedom
    ## Multiple R-squared:  0.6445, Adjusted R-squared:  0.6363 
    ## F-statistic: 77.97 on 1 and 43 DF,  p-value: 3.273e-11
    e2=m2$resid
  4. 所配適之模型為何? \(\hat{y^*}\) = 6.4027 +0.0298\(obs1\)

  5. 檢定殘差是否具常態性?

    qqnorm(e2)
    qqline(e2)

    shapiro.test(e2)
    ## 
    ##  Shapiro-Wilk normality test
    ## 
    ## data:  e2
    ## W = 0.9766, p-value = 0.4887

    \(H_0\):殘差具常態性
    \(H_1\):殘差不具常態性
    因為shapiro.test結果之p值大,無法推翻虛無假設,表示殘差具常態性

  6. 檢定殘差是否具一階自我相關?

    durbinWatsonTest(m2)
    ##  lag Autocorrelation D-W Statistic p-value
    ##    1       0.2586416      1.478482   0.062
    ##  Alternative hypothesis: rho != 0
    dwtest(m2)
    ## 
    ##  Durbin-Watson test
    ## 
    ## data:  m2
    ## DW = 1.4785, p-value = 0.02988
    ## alternative hypothesis: true autocorrelation is greater than 0

    \(H_0\):殘差無一階自我相關
    \(H_1\):殘差有一階自我相關
    由於兩種檢定結果之p值小,所以推翻虛無假設,表示殘差有一階自我相關

  7. 做均齊性變異數檢定。

    ncvTest(m2)
    ## Non-constant Variance Score Test 
    ## Variance formula: ~ fitted.values 
    ## Chisquare = 20.97596, Df = 1, p = 4.6508e-06
    bptest(m2)
    ## 
    ##  studentized Breusch-Pagan test
    ## 
    ## data:  m2
    ## BP = 20.282, df = 1, p-value = 6.682e-06

    \(H0\):變異數具均齊性(變異數為常數)
    \(H1\):變異數不具均齊性
    ncvTest 檢定結果之p值小,推翻虛無假設,表示變異數不具均齊性,所以變異數不為常數
    bptest 檢定結果之p值小,推翻虛無假設,表示變異數不具均齊性,所以變異數不為常數

  1. 用第(2)小題的模型檢驗是否有離群值或影響點
  2. 去除離群值或影響點後,重做 (2c) ~ (2g)