library(haven)
curvilinear_exercise <- read_sav("Box Sync/Courses/2019 Fall/BER642/Exercise/4/curvilinear_exercise.sav")
curvreg_data <- curvilinear_exercise
head(curvreg_data)
## # A tibble: 6 x 4
##       y     x    X2    X3
##   <dbl> <dbl> <dbl> <dbl>
## 1     4     2     4     8
## 2     6     2     4     8
## 3     5     2     4     8
## 4     7     4    16    64
## 5    10     4    16    64
## 6    10     4    16    64
plot(curvreg_data$x,curvreg_data$y, main="Scatterplot Between Practice Time and  Task Score",xlab="Practice Time",ylab="Score")

curvreg_lm <- lm(y~x,data=curvreg_data)
summary(curvreg_lm)
## 
## Call:
## lm(formula = y ~ x, data = curvreg_data)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -2.7424 -1.6130  0.1245  0.9356  5.6245 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)   3.8646     1.0772   3.588   0.0021 ** 
## x             1.4389     0.1274  11.295 1.33e-09 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 2.112 on 18 degrees of freedom
## Multiple R-squared:  0.8764, Adjusted R-squared:  0.8695 
## F-statistic: 127.6 on 1 and 18 DF,  p-value: 1.329e-09
plot(y~x,data=curvreg_data)
abline(lm(y~x,data=curvreg_data))

curvreg_Quadm <- lm(y~x+X2,data=curvreg_data)
summary(curvreg_Quadm)
## 
## Call:
## lm(formula = y ~ x + X2, data = curvreg_data)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -2.8418 -0.6870 -0.1126  0.6913  3.8206 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) -1.77726    1.36506  -1.302 0.210296    
## x            3.43995    0.42058   8.179  2.7e-07 ***
## X2          -0.13380    0.02754  -4.858 0.000148 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.406 on 17 degrees of freedom
## Multiple R-squared:  0.9482, Adjusted R-squared:  0.9421 
## F-statistic: 155.7 on 2 and 17 DF,  p-value: 1.175e-11
library(ggplot2)
ggplot(curvreg_data, aes(x=x, y=y)) + geom_point()+stat_smooth(se=F, method='lm', formula=y~poly(x,2))

curvreg_cubreg <- lm(y~x+X2+X3,data=curvreg_data)
summary(curvreg_cubreg)
## 
## Call:
## lm(formula = y ~ x + X2 + X3, data = curvreg_data)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -2.6033 -0.7887  0.0367  0.5494  3.7376 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)  
## (Intercept) -0.3653250  2.7034153  -0.135    0.894  
## x            2.5878285  1.4631647   1.769    0.096 .
## X2           0.0002056  0.2217818   0.001    0.999  
## X3          -0.0060313  0.0099021  -0.609    0.551  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.433 on 16 degrees of freedom
## Multiple R-squared:  0.9494, Adjusted R-squared:  0.9399 
## F-statistic: 100.1 on 3 and 16 DF,  p-value: 1.403e-10
ggplot(curvreg_data, aes(x=x, y=y)) + geom_point()+stat_smooth(se=F, method='lm', formula=y~poly(x,3))

anova(curvreg_lm,curvreg_Quadm)
## Analysis of Variance Table
## 
## Model 1: y ~ x
## Model 2: y ~ x + X2
##   Res.Df    RSS Df Sum of Sq      F    Pr(>F)    
## 1     18 80.273                                  
## 2     17 33.614  1    46.659 23.597 0.0001477 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
anova(curvreg_Quadm,curvreg_cubreg)
## Analysis of Variance Table
## 
## Model 1: y ~ x + X2
## Model 2: y ~ x + X2 + X3
##   Res.Df    RSS Df Sum of Sq     F Pr(>F)
## 1     17 33.614                          
## 2     16 32.852  1   0.76176 0.371  0.551
anova(curvreg_lm,curvreg_cubreg)
## Analysis of Variance Table
## 
## Model 1: y ~ x
## Model 2: y ~ x + X2 + X3
##   Res.Df    RSS Df Sum of Sq      F   Pr(>F)    
## 1     18 80.273                                 
## 2     16 32.852  2    47.421 11.548 0.000787 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1