Q1.(i) How many single-person households are there in the data set?
k401ksubs <- read_excel("/Users/jingwu/Desktop/401ksubs.xls")
sum(k401ksubs$fsize==1)
## [1] 2017
single_house <- subset(k401ksubs, fsize==1)
fit_ksubs <- lm(nettfa~inc+age, data = `single_house`)
summary(fit_ksubs)
##
## Call:
## lm(formula = nettfa ~ inc + age, data = single_house)
##
## Residuals:
## Min 1Q Median 3Q Max
## -179.95 -14.16 -3.42 6.03 1113.94
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -43.03981 4.08039 -10.548 <2e-16 ***
## inc 0.79932 0.05973 13.382 <2e-16 ***
## age 0.84266 0.09202 9.158 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 44.68 on 2014 degrees of freedom
## Multiple R-squared: 0.1193, Adjusted R-squared: 0.1185
## F-statistic: 136.5 on 2 and 2014 DF, p-value: < 2.2e-16
The coefficient is positive but not that large as what I would expected.
Considering we are taking age as one control, 0 age may not be really meaningful in this case.
pnorm(-1.709846)
## [1] 0.04364718
thus it’s not enough to reject at 1% significance level.
single_house <- subset(k401ksubs, fsize==1)
fit_ksubs <- lm(nettfa~inc, data = `single_house`)
summary(fit_ksubs)
##
## Call:
## lm(formula = nettfa ~ inc, data = single_house)
##
## Residuals:
## Min 1Q Median 3Q Max
## -185.12 -12.85 -4.85 1.78 1112.66
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -10.5709 2.0607 -5.13 3.18e-07 ***
## inc 0.8207 0.0609 13.48 < 2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 45.59 on 2015 degrees of freedom
## Multiple R-squared: 0.08267, Adjusted R-squared: 0.08222
## F-statistic: 181.6 on 1 and 2015 DF, p-value: < 2.2e-16
The coefficient didn’t change a lot though the intercept changed.
Q2. In R, use a seed of 123 and generate n = 1000 samples, if we want to test β2 − β1 = 0, we are trying to see β1 = β2, and β1x1 + β2x2 = β1*(x1+x2), that’s why we create w = x1 + x2 and regress y on w and x1.
set.seed(123)
n <- 1000
X1 <- rnorm(n,mean=5,sd=sqrt(10))
X2 <- 2.5+3*(X1)+rnorm(n,mean=0,sd=1)
Y <- 10+2*(X1)+2*(X2)+rnorm(n,mean=0,sd=1)
w <- (X1)+(X2)
data <- data.frame(Y,X1,X2,w)
model_1 <- lm(Y~X1+X2, data=data)
model_2 <- lm(Y~X1+w, data=data)
summary(model_1)
##
## Call:
## lm(formula = Y ~ X1 + X2, data = data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -2.8360 -0.6277 -0.0370 0.6538 3.3787
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 9.94428 0.09441 105.33 <2e-16 ***
## X1 1.91068 0.09373 20.38 <2e-16 ***
## X2 2.02751 0.03079 65.86 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.9788 on 997 degrees of freedom
## Multiple R-squared: 0.9985, Adjusted R-squared: 0.9985
## F-statistic: 3.344e+05 on 2 and 997 DF, p-value: < 2.2e-16
summary(model_2)
##
## Call:
## lm(formula = Y ~ X1 + w, data = data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -2.8360 -0.6277 -0.0370 0.6538 3.3787
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 9.94428 0.09441 105.333 <2e-16 ***
## X1 -0.11682 0.12439 -0.939 0.348
## w 2.02751 0.03079 65.860 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.9788 on 997 degrees of freedom
## Multiple R-squared: 0.9985, Adjusted R-squared: 0.9985
## F-statistic: 3.344e+05 on 2 and 997 DF, p-value: < 2.2e-16
Q3. See attached picture.