어떤 슈퍼마켓에서 고객이 구입하는 상품의 금액과 카운터에서 값을 치르는 데 걸리는 시간 사이에 회귀함수관계가 있는가를 알아보기 위하여 10명의 고객을 임의로 추출하여 다음의 데이터를 얻었다
x <- c(6.4, 16.1, 42.1, 2.1, 30.7, 32.1, 7.2, 3.4, 20.8, 1.5)
y <- c(1.7, 2.7, 4.9, 0.3, 3.9, 4.1, 1.2, 0.5, 3.3, 0.2)
data <- data.frame(y,x)
plot(y~x, data=data)
fit <- lm(y~x)
summary(fit)$r.squared
## [1] 0.954247
값은 0.954 이므로, 모형이 충분히 설명력이 있다고 볼 수 있다.
sqrtx <- sqrt(x)
sqrty <- sqrt(y)
revx <- 1/x
revy <- 1/y
logy <- log(y)
logx <- log(x)
# (a)
a <- lm(logy~x)
#(b)
b <- lm(y~sqrtx)
#(c)
c <- lm(logy~logx)
#(d)
d <- lm(logy~x)
#(e)
e <- lm(y~revx)
# 위의 모형을 적절한 모형변환을 통하여 선형모형으로 만든 후 회귀모형을 적합하고 각각 r2을 구하시오.
summary(a)$r.squared
## [1] 0.7384658
summary(b)$r.squared
## [1] 0.988811
summary(c)$r.squared
## [1] 0.9624502
summary(d)$r.squared
## [1] 0.7384658
summary(e)$r.squared
## [1] 0.6873067
어떤 모형이 가장 큰 r 2의 값을 가지는가?
==> (b)의 경우일 때 모형이 가장 높은 설명력을 가진다.
위의 (3)에서 r2 의 값이 가장 큰 모형이 선택되었을 경우,이 모형의 추정식을 사용하여 구매상품의 총금액이 10,000원인 경우에, 카운터에서 값을 치르는 데 평균 몇 분이 소요되리라고 예측하는가?
plot(y~sqrtx)
coef(b)
## (Intercept) sqrtx
## -0.9636011 0.9010260
모형의 추정식 hat{y} = -0.9636011 + 0.9010260x
yhat <- -0.9636011 + 0.9010260 * sqrt(10000/1000)
yhat
## [1] 1.885693
평균 1.89분 소요될 것이다.
아마존 강 수위 문제 (문제의 출처 : 참고문헌(3.8)) 아마존 강 유역은 지구상의 가장 큰 열대림 지역이지만 대부분의 다른 자연자원과 마찬가지로 개발의 손길이 미치면서 열대림이 급속히 파괴됐다. 1970 년대 이후 아마존 상류지역에 도로가 건설되면서 인구가 빠르게 증가되었고 대규모의 삼림파괴가 이뤄졌다. 강수량과 유수량이 모두 영향을 받을 수 있기 때문에 이것은 결국 아마존 강 전체에 영향을 미치는 심각한 기후학적 및 수문학적 변화를 가져왔다. 다음의 표는 페루 이키토스(Iquitos)에서 1962년부터 1978년까지 기록한 아마존 강 최고수위 (High)와, 최저수위 (Low)를 기록한 것이다 (단위: 미터).
year <- c(1962:1978)
high <- c(25.82, 25.35, 24.29, 24.05, 24.89, 25.35, 25.23, 25.06, 27.13, 27.36, 26.65, 27.13, 27.49, 27.08, 27.51, 27.54, 26.21)
low <- c(18.24, 16.50, 20.26, 20.97, 19.43, 19.31, 20.85, 19.54, 20.49, 21.91, 22.51, 18.81, 19.42, 19.10, 18.80, 18.80, 17.57)
962년부터 1969년까지의 데이터는 개발 이전에 수집된 데이터이고, 1970년부터 1978년까지의 데이터는 개발이후에 관측된 데이터를 나타낸다. 이 데이터는 아마존 상류지역의 삼림파괴가 아마존 유역의 강 수위에 변화를 일으켰는지 분석하고자 한다. 우리의 관심은 시간에 따른 아마존 강 수위 변화여부이다. 예를 들어, 우리가 다음을 적합한다면,
High = β0 + β1 × Year + ϵ
par(mfrow=c(1,3))
plot(high~year)
plot(low~year)
plot(high~low)
model1 <- lm(high~year) #Year에 대한 High
summary(model1)
##
## Call:
## lm(formula = high ~ year)
##
## Residuals:
## Min 1Q Median 3Q Max
## -1.3629 -0.5341 0.1479 0.4903 1.1412
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -330.21235 78.03319 -4.232 0.000725 ***
## year 0.18088 0.03961 4.567 0.000371 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.8001 on 15 degrees of freedom
## Multiple R-squared: 0.5816, Adjusted R-squared: 0.5537
## F-statistic: 20.85 on 1 and 15 DF, p-value: 0.0003708
coef(model1)
## (Intercept) year
## -330.2123529 0.1808824
model2 <- lm(low~year) #Year에 대한 Low
summary(model2)
##
## Call:
## lm(formula = low ~ year)
##
## Residuals:
## Min 1Q Median 3Q Max
## -3.1147 -0.7121 -0.1610 0.9306 2.9664
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 35.106961 151.723912 0.231 0.82
## year -0.007892 0.077017 -0.102 0.92
##
## Residual standard error: 1.556 on 15 degrees of freedom
## Multiple R-squared: 0.0006996, Adjusted R-squared: -0.06592
## F-statistic: 0.0105 on 1 and 15 DF, p-value: 0.9197
coef(model2)
## (Intercept) year
## 35.106960784 -0.007892157
model3 <- lm(high~low) #Low에 대한 High
summary(model3)
##
## Call:
## lm(formula = high ~ low)
##
## Residuals:
## Min 1Q Median 3Q Max
## -2.05605 -0.87774 0.05615 1.01720 1.40344
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 26.40088 4.02478 6.560 9.05e-06 ***
## low -0.01406 0.20520 -0.069 0.946
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1.237 on 15 degrees of freedom
## Multiple R-squared: 0.0003129, Adjusted R-squared: -0.06633
## F-statistic: 0.004695 on 1 and 15 DF, p-value: 0.9463
coef(model3)
## (Intercept) low
## 26.40087963 -0.01405959
anova(model1)
## Analysis of Variance Table
##
## Response: high
## Df Sum Sq Mean Sq F value Pr(>F)
## year 1 13.3491 13.3491 20.853 0.0003708 ***
## Residuals 15 9.6023 0.6402
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
anova(model2)
## Analysis of Variance Table
##
## Response: low
## Df Sum Sq Mean Sq F value Pr(>F)
## year 1 0.025 0.02541 0.0105 0.9197
## Residuals 15 36.301 2.42010
anova(model3)
## Analysis of Variance Table
##
## Response: high
## Df Sum Sq Mean Sq F value Pr(>F)
## low 1 0.0072 0.00718 0.0047 0.9463
## Residuals 15 22.9442 1.52962
plot(high~year)
year_1960 <- c(1962:1969)
year_1970 <- c(1970:1979)
high_1960 <- high[1:8]
high_1970 <- high[9:17]
low_1960 <- low[1:8]
low_1970 <- low[9:17]
g <- rep(c('1960','1970'), c(8,9))
dt <- data.frame(high,low,g)
full_model1960 <- lm(high_1960~low_1960)
full_model1970 <- lm(high_1970~low_1970)
reduced_model <- lm(high~low)
plot(high~low, data=data, col=as.factor(dt$g),pch=16)
legend("bottomleft",legend = c('1960','1970'),col=1:2,pch=16)
abline(full_model1960, col=1, lty=2) #black
abline(full_model1970, col=2, lty=2) #red
abline(reduced_model, col=3, lty=2) #green
가설 설정 H0 : β01 = β02 and β11 = β12 H1 : β01 ≠ β02 or β11 ≠ β12
알파 = 0.01
검정통계량 F값
기각역 : FO > F0.01(2, 13) = 6.700965
축소모형
reduced_SSE <- anova(reduced_model)[2,2]
reduced_DF <- anova(reduced_model)[2,1]
SSE1 <- anova(full_model1960)[2,2]
SSE2 <- anova(full_model1970)[2,2]
DF1 <- anova(full_model1960)[2,1]
DF2 <- anova(full_model1970)[2,1]
풀모형
full_SSE <- SSE1+SSE2
full_DF <- DF1+DF2
F0 <- ((reduced_SSE-full_SSE)/(reduced_DF-full_DF)) / (full_SSE/full_DF)
Fa <- qf(0.01,2,13, lower.tail=F)
print(F0)
## [1] 42.82745
print(Fa)
## [1] 6.700965
F0>Fa
## [1] TRUE
F0 [42.82745] +>Fa [6.700965] 이기 때문에, 귀무가설을 기각할 수 있다. 따라서 두 회귀모형은 다르다고 할 수 있다.
y1<-dt[dt$g=='1960', 1]
y2<-dt[dt$g=='1970', 1]
x1<-dt[dt$g=='1960', 2]
x2<-dt[dt$g=='1970', 2]
Sxx_1 <- sum((x1 - mean(x1))^2)
Sxx_2 <- sum((x2 - mean(x2))^2)
full_MSE <- full_SSE/full_DF
var_hat <- full_MSE * (1/Sxx_1 + 1/Sxx_2)
H0 : β11 = β12 H1 : β11 ≠ β12
유의수준 : 0.01
검정통계량 : T 값
검정통계량 관측값 : T0 = -1.627824
기각역 : T0 < T0.005(13) = 3.012276
#검정통계량 관측값
t0 <- (coef(full_model1960)[2] - coef(full_model1970)[2]) / sqrt(var_hat)
ta <- qt(0.995,full_DF)
유의 수준 하에서 귀무가설을 기각할 수 있음. 두 모형의 기울기는 통계적으로 같다고 할 수 있다.