3.7참게가 적어도 한 마리의 부수체를 가지면 Y=1이라고 하고 그렇지 않으면 Y=0이라고 하자. 무게를 예측변수로 이용해서 선형확률모형을 적합하라.
a. 보통의 최소제곱법을 이용하여 모수를 추정하고 추정값들을 해석하라. 가장 큰 무게 값 5.20kg에 대하여 확률을 예측하고 결론을 내려라.
lm 함수를 이용하여 모형을 적합한다.
crab<-read.table('crab.txt',header=TRUE)
crab$weight<-crab$weight/1000 #weigth 변수의 단위를 kg으로 변경
result1<-lm(satell~weight,data=crab)
summary(result1)
Call:
lm(formula = satell ~ weight, data = crab)
Residuals:
Min 1Q Median 3Q Max
-4.4559 -2.2397 -0.8295 1.6449 11.3573
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -1.9911 0.9710 -2.050 0.0418 *
weight 2.0147 0.3878 5.196 5.75e-07 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 2.934 on 171 degrees of freedom
Multiple R-squared: 0.1363, Adjusted R-squared: 0.1313
F-statistic: 27 on 1 and 171 DF, p-value: 5.754e-07
최소 제곱법을 이용하기 위해 단선 선형회귀를 실시하였다. 절편은 -1.9910579,계수는 2.0146692이다. 계수의 p-value는 5.754022110^{-7}로 유의하다. 회귀식은 \(\small satell=-1.9911+2.0147 \times weight\)이다. 이 식에서 weight에 5.20을 대입하면, satell이 fitted value는 약 8.4이며, 실제 관측값 7과 약 1.4의 차이가 난다.
b. Y를 이항변수로 간주하고 ML 방법으로 모형을 적합하라.
bisatell<-ifelse(crab$satell>0,1,0) #satell가 0보다 크면 1로, 그렇지 않으면 0으로 변환하고, 'bisatell' 객체에 저장한다.
crab1<-subset(crab,select=-satell) #기존 데이터프레임에서 satell 삭제한다.
crab2<-cbind(crab1,bisatell)
glm(bisatell~weight,family = quasi(),data=crab2)
Call: glm(formula = bisatell ~ weight, family = quasi(), data = crab2)
Coefficients:
(Intercept) weight
-0.1449 0.3227
Degrees of Freedom: 172 Total (i.e. Null); 171 Residual
Null Deviance: 39.78
Residual Deviance: 33.82 AIC: NA
glm 함수에서 family=binomial로 지정시, 연결함수를 항등연결로 시킬 수 있는 옵션이 없다.(binomial에서는 logit,probit,cauchit만 가능하다). 차선책으로 분포가 확실하지 않을 때 유사가능도 모형인 quasi 함수를 사용하였다. 그 결과 적합된 모형은 \(\small Prob(Y)=-0.1449+0.3227 \times weight\)이며, weight가 5.2kg 일때 fitted value는 1.53으로, 확률이 가질 수 있는 최대값인 1을 초과한다.
c.로지스틱 회귀모형을 적합하라.
result2<-glm(bisatell~weight,family=binomial(link = 'logit'),data=crab2)
summary(result2)
Call:
glm(formula = bisatell ~ weight, family = binomial(link = "logit"),
data = crab2)
Deviance Residuals:
Min 1Q Median 3Q Max
-2.1108 -1.0749 0.5426 0.9122 1.6285
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) -3.6947 0.8802 -4.198 2.70e-05 ***
weight 1.8151 0.3767 4.819 1.45e-06 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
(Dispersion parameter for binomial family taken to be 1)
Null deviance: 225.76 on 172 degrees of freedom
Residual deviance: 195.74 on 171 degrees of freedom
AIC: 199.74
Number of Fisher Scoring iterations: 4
적합된 로지스틱 모형은 \(log(\frac{\hat{\pi}}{1-\hat{\pi}})=-3.6947264+1.8151446 \times weight\)이다. weight에 5.2를 대입하면 \(log(\frac{\hat{\pi}}{1-\hat{\pi}})=5.743\)이며, 이것을 \(\hat{\pi}\)에 대해 풀면 \(\hat{\pi}=0.9968\)이다. 즉 무게가 5.2kg일 경우 부수체를 가질 확률이 아주 높다.
3.13
a.무게를 예측변수, 부수체를 반응변수로 간주하고 포아송 로그 회귀식을 적합하라.
result3<-glm(satell~weight,family=poisson(),data=crab)
summary(result3)
Call:
glm(formula = satell ~ weight, family = poisson(), data = crab)
Deviance Residuals:
Min 1Q Median 3Q Max
-2.9307 -1.9981 -0.5627 0.9298 4.9992
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) -0.42841 0.17893 -2.394 0.0167 *
weight 0.58930 0.06502 9.064 <2e-16 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
(Dispersion parameter for poisson family taken to be 1)
Null deviance: 632.79 on 172 degrees of freedom
Residual deviance: 560.87 on 171 degrees of freedom
AIC: 920.16
Number of Fisher Scoring iterations: 5
적합된 회귀식은 \(\small log(\mu)=-0.42841+0.58930 \times weight\)이다.
b.암참게의 평균무게가 2.44kg 일 때 부수체의 평균수를 구하여라
\(\small exp[-0.42841+0.58930 \times 2.44]=2.74\)
c. \(\hat{\beta}\)을 이용하여 무게의 효과를 설명하라. 95% 신뢰구간을 구하라.
\(\hat{\beta}\)이 \(\small exp[0.58930]\)=1.8027261이며, 무게가 1kg 증가할 때 부수체의 수는 약 1.8배 증가한다. \(\hat{\beta}\)의 95% 신뢰구간은 0.4597002, 0.7144983이므로, 승법효과의 신뢰구간은 (1.5835992,2.0431614)이다.
d.부수체수와 무게의 독립성을 왈드 검정으로 실시하라.
library(lmtest)
waldtest(result3,test = 'Chisq')
Wald test
Model 1: satell ~ weight
Model 2: satell ~ 1
Res.Df Df Chisq Pr(>Chisq)
1 171
2 172 -1 82.155 < 2.2e-16 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
\(\chi^{2}\)통계량은 82.155이고, p-value는 0에 가깝다. 즉 부수체수와 무게는 독립이 아니다.
e.무게의 효과에 대한 가능도비 검정을 하고 결과를 해석하라.
anova(result3,test='LRT')
Analysis of Deviance Table
Model: poisson, link: log
Response: satell
Terms added sequentially (first to last)
Df Deviance Resid. Df Resid. Dev Pr(>Chi)
NULL 172 632.79
weight 1 71.925 171 560.87 < 2.2e-16 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
\(\chi^{2}\)통계량은 71.925이고, p-value는 0에 가깝다. 즉 귀무가설(\(\beta=0\))을 기각하며, 부수체수를 추정하는데 있어 무게는 유의미한 설명변수다.