Data and setup

library(data.table)
library(rstudioapi)
docPath <- rstudioapi::documentPath()
codePath <- dirname(docPath)
projPath <- dirname(codePath)
library(ggplot2)
source("~/Econometrics/Hw1/Data/MaxwellDavidoff_HW1_create_data.R")
dt2 <- as.data.frame(saved_data)
head(dt2)
##          x1       x2       x3        x4          y
## 1 -4.689827 3.123235 2.226993 11.205968 -0.5972656
## 2 -2.557522 3.739444 2.222386 12.417888 -0.2714289
## 3  1.457067 2.533134 1.825844  8.399975  1.6910782
## 4  8.164156 4.819952 2.042146 13.810266  3.4844145
## 5 -5.966361 1.473426 2.013879  7.002562 -0.4064829
## 6  7.967794 1.156400 1.667470  5.093257  5.6775954
tail(dt2)
##             x1       x2       x3        x4          y
## 995  -7.316605 2.350013 2.184705  9.472963 -1.1584851
## 996   5.536680 4.100113 1.998389 12.193784  2.6431093
## 997   2.718997 1.276281 2.206757  7.422337  4.9300575
## 998  -4.357274 1.963797 1.840175  7.313837 -0.5931730
## 999  -6.174811 1.971207 2.200847  8.786140 -0.4313542
## 1000 -4.689266 2.548452 1.937605  8.851218 -1.2465480
summary(dt2)
##        x1                  x2              x3              x4        
##  Min.   :-9.973707   Min.   :1.002   Min.   :1.349   Min.   : 4.423  
##  1st Qu.:-4.837427   1st Qu.:1.913   1st Qu.:1.862   1st Qu.: 7.819  
##  Median :-0.334809   Median :2.924   Median :1.993   Median : 9.965  
##  Mean   :-0.006167   Mean   :2.961   Mean   :1.997   Mean   : 9.953  
##  3rd Qu.: 4.938649   3rd Qu.:4.028   3rd Qu.:2.147   3rd Qu.:12.075  
##  Max.   : 9.998612   Max.   :4.996   Max.   :2.728   Max.   :16.327  
##        y          
##  Min.   :-6.8138  
##  1st Qu.:-1.4044  
##  Median : 0.9719  
##  Mean   : 1.0675  
##  3rd Qu.: 3.6435  
##  Max.   : 9.0832

Regression Results 1

lm1 <- lm(dt2$y ~ dt2$x1)
summary(lm1)
## 
## Call:
## lm(formula = dt2$y ~ dt2$x1)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
## -4.004 -1.019  0.014  1.050  5.217 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 1.070580   0.044974   23.80   <2e-16 ***
## dt2$x1      0.502159   0.007801   64.37   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.422 on 998 degrees of freedom
## Multiple R-squared:  0.8059, Adjusted R-squared:  0.8057 
## F-statistic:  4143 on 1 and 998 DF,  p-value: < 2.2e-16
confint(lm1, "dt2$x1", level = .95 )
##            2.5 %    97.5 %
## dt2$x1 0.4868502 0.5174682

Plots 1

ggplot(data = dt2, aes(x1, y)) +geom_point() + geom_smooth(method = "lm")
## `geom_smooth()` using formula = 'y ~ x'

par(mfrow = c(2,3))
plot(lm1, c(1:6))

- Scatter plot shows a positive linear relationship between y and x1.

Regression Results 2

lm2 <- lm(dt2$y ~ dt2$x2)
summary(lm2)
## 
## Call:
## lm(formula = dt2$y ~ dt2$x2)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -6.6508 -2.4596 -0.1054  2.4963  7.2317 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  4.13585    0.25411   16.28   <2e-16 ***
## dt2$x2      -1.03619    0.07967  -13.01   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 2.985 on 998 degrees of freedom
## Multiple R-squared:  0.1449, Adjusted R-squared:  0.1441 
## F-statistic: 169.1 on 1 and 998 DF,  p-value: < 2.2e-16
confint(lm2, "dt2$x2", level = .95 )
##            2.5 %     97.5 %
## dt2$x2 -1.192538 -0.8798505

Plots 2

ggplot(data = dt2, aes(x2, y)) +geom_point() + geom_smooth(method = "lm")
## `geom_smooth()` using formula = 'y ~ x'

par(mfrow = c(2,3))
plot(lm2, c(1:6))

- Scatter plot shows a negative linear relationship between y and x2. Variance a wider comparatively to scatter plot for x1.

Regression Results 3

lm3 <- lm(dt2$y ~ dt2$x3)
summary(lm3)
## 
## Call:
## lm(formula = dt2$y ~ dt2$x3)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -7.0796 -2.4682 -0.0848  2.4745  6.8311 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  -5.6362     0.9624  -5.857 6.41e-09 ***
## dt2$x3        3.3573     0.4794   7.004 4.58e-12 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 3.151 on 998 degrees of freedom
## Multiple R-squared:  0.04685,    Adjusted R-squared:  0.04589 
## F-statistic: 49.05 on 1 and 998 DF,  p-value: 4.581e-12
confint(lm3, "dt2$x3", level = .95 )
##           2.5 %   97.5 %
## dt2$x3 2.416612 4.297993

Plots 3

ggplot(data = dt2, aes(x3, y)) +geom_point() + geom_smooth(method = "lm")
## `geom_smooth()` using formula = 'y ~ x'

par(mfrow = c(2,3))
plot(lm3, c(1:6))

Regression Results 4

lm4 <- lm(dt2$y ~ dt2$x4)
summary(lm4)
## 
## Call:
## lm(formula = dt2$y ~ dt2$x4)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -7.5363 -2.4189 -0.1038  2.3935  7.7938 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)   4.6278     0.3945  11.731   <2e-16 ***
## dt2$x4       -0.3577     0.0384  -9.316   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 3.096 on 998 degrees of freedom
## Multiple R-squared:  0.08001,    Adjusted R-squared:  0.07909 
## F-statistic: 86.79 on 1 and 998 DF,  p-value: < 2.2e-16
confint(lm4, "dt2$x4", level = .95 )
##             2.5 %     97.5 %
## dt2$x4 -0.4330831 -0.2823814

Plots 4

ggplot(data = dt2, aes(x4, y)) +geom_point() + geom_smooth(method = "lm")
## `geom_smooth()` using formula = 'y ~ x'

par(mfrow = c(2,3))
plot(lm4, c(1:6))

- Scatter plot shows a negative linear relationship between y and x4.