経済データでは、従属変数・独立変数共に確率変数であると考えるのが自然。
単回帰モデルには以下の仮定がある。
\(\beta_0, \beta_1\)がパラメータだとすると、最小2乗法では当てはめ値の差の2乗の和を最小にするようにパラメータを推定する。\[\min_{b_0, b_1} \sum_{i=1}^{n} (y_i - b_0 - b_1 x_i)^2\]という最適化問題を解くことになる。
\(y_i\)の回帰式は\(\hat{y}_i = \hat{\beta}_0 + \hat{\beta}_1 x_i\)であったから、\[y_i = \hat{y}_i + \hat{u}_i\]が成り立つ。\[\begin{aligned} {\rm SST} &= \sum_{i=1}^n (y_i - \overline{y})^2 \\ {\rm SSE} &= \sum_{i=1}^n (\hat{y}_i - \overline{y})^2 \\ {\rm SSR} &= \sum_{i=1}^n \hat{u}_{i}^{2} \\ {\rm SST} &= {\rm SSE} + {\rm SSR} \end{aligned} \] ここで、SST = Total sum of squares (全変動)、SSE = Explained sum of squares (独立変数によって説明された変動)、SSR = Residual sum of squares (残差の変動)である。
決定係数\(R^2\)は、\[R^2 = \frac{\rm SSE}{\rm SST} = 1 - \frac{\rm SSR}{\rm SST} \]
仮定3.1 = 3.6の下で、\[\frac{\hat{\beta}_j - \beta_j}{{\rm se} \hat{\beta}_j} \sim t(n-2)\]が成り立つ。
係数が0かどうかの検定
library(ggplot2)
データを読み込む
cvdata <- read.csv("cv.csv", row.names = 1)
散布図を書く
g <- ggplot(cvdata, aes (x=conv, y=pop)) +
geom_point(shape=1, size=3, na.rm=TRUE) +
xlab("Population") + ylab("Number of store") + ggtitle("Scatter plot") +
theme_bw()
g
回帰分析を行う
result <- lm(conv~pop, data=cvdata)
summary(result)
##
## Call:
## lm(formula = conv ~ pop, data = cvdata)
##
## Residuals:
## Min 1Q Median 3Q Max
## -605.69 -82.42 0.93 82.99 859.51
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -106.62323 47.98638 -2.222 0.0314 *
## pop 0.37372 0.01283 29.125 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 225.9 on 45 degrees of freedom
## Multiple R-squared: 0.9496, Adjusted R-squared: 0.9485
## F-statistic: 848.3 on 1 and 45 DF, p-value: < 2.2e-16
散布図に回帰直線を引く
g <- ggplot(cvdata, aes(x=pop, y=conv)) +
geom_point(shape=1, size=3, na.rm=TRUE) +
xlab("Population") + ylab("Number of store") + ggtitle("Scatter plot") +
stat_smooth(method="lm", se=FALSE) +
theme_bw()
g
各プロットに名前をつける
g <- ggplot(cvdata, aes(x=pop, y=conv)) +
geom_point(shape=1, size=3, na.rm=TRUE) +
xlab("Population") + ylab("Number of store") + ggtitle("Scatter plot") +
stat_smooth(method="lm", se=FALSE) +
geom_text(aes(label=rownames(cvdata)), size=3, vjust=1) +
theme_bw()
g
これだと見にくいので、人口の大きな都道府県に限定する
g <- ggplot(cvdata, aes(x=pop, y=conv)) +
geom_point(shape=1, size=3, na.rm=TRUE) +
xlab("Population") + ylab("Number of store") + ggtitle("Scatter plot") +
stat_smooth(method="lm", se=FALSE) +
geom_text(data=subset(cvdata, pop>3500), aes(label=rownames(subset(cvdata, pop>3500))), size=3, vjust=1, hjust=1) +
theme_bw()
g