Question 1: 0.02575263 error rate
d: for a training set of 30% the error rate is 0.0265 for 40% its 0.025875 for 50% its 0.0288
f: adding the student section does not seem to have altered the error rate much and now is at 0.024875
Qeustion 6: d: the estimated standard error for income and balance using the glm was 4.985e-06 and 2.274e-04 respectively. the standard error using the bootstrap method for income and balance respectively was 4.988746e-06 and 2.347490e-04. because these values are similar between the two different methods used the assumptions the glm uses (I.I.D) is likley accurate and the sample size is also suffecient. it also might show that there isn’t irregular values with the data such as outliers
Question 7 c: no, the model did not accurately predict the first observation e: the error rate was 0.4499541 which, for a binary prediction of only two options is very similar to random guessing
Weekly <- read.csv("C:\\Users\\jwilk\\Documents\\DATA Science\\DATA_5321\\Assignment_3\\Weekly.csv")
Weekly$Direction <- ifelse(Weekly$Direction == "Up", 1,0)
weekly_indexed <- Weekly[-1, ]
weekly_glm <- glm(Direction ~ Lag1 + Lag2, data = Weekly, family = binomial)
summary(weekly_glm)
##
## Call:
## glm(formula = Direction ~ Lag1 + Lag2, family = binomial, data = Weekly)
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 0.22122 0.06147 3.599 0.000319 ***
## Lag1 -0.03872 0.02622 -1.477 0.139672
## Lag2 0.06025 0.02655 2.270 0.023232 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## (Dispersion parameter for binomial family taken to be 1)
##
## Null deviance: 1496.2 on 1088 degrees of freedom
## Residual deviance: 1488.2 on 1086 degrees of freedom
## AIC: 1494.2
##
## Number of Fisher Scoring iterations: 4
weekly_glm_indexed <- glm(Direction ~ Lag1 + Lag2, data = weekly_indexed, family = binomial)
summary(weekly_glm_indexed)
##
## Call:
## glm(formula = Direction ~ Lag1 + Lag2, family = binomial, data = weekly_indexed)
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 0.22324 0.06150 3.630 0.000283 ***
## Lag1 -0.03843 0.02622 -1.466 0.142683
## Lag2 0.06085 0.02656 2.291 0.021971 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## (Dispersion parameter for binomial family taken to be 1)
##
## Null deviance: 1494.6 on 1087 degrees of freedom
## Residual deviance: 1486.5 on 1085 degrees of freedom
## AIC: 1492.5
##
## Number of Fisher Scoring iterations: 4
pred_LOOCV <- predict(weekly_glm_indexed, Weekly[1,], type = "response")
pred_LOOCV <- ifelse(pred_LOOCV > 0.5, 1,0)
pred_LOOCV
## 1
## 1
n = nrow(Weekly)
print(n)
## [1] 1089
correct_sum <- 0
for (i in 1:n) {
weekly_glm_indexed <- glm(Direction ~ Lag1 + Lag2, data = Weekly[-i,], family = binomial)
actual_value <- Weekly[i,]$Direction
pred_LOOCV <- predict(weekly_glm_indexed, Weekly[i,], type = "response")
pred_LOOCV <- ifelse(pred_LOOCV > 0.5, 1,0)
if (pred_LOOCV == actual_value) {
correct_sum <- correct_sum + 1
}
}
print(correct_sum)
## [1] 599
error_rate <- (1 - correct_sum/n)
error_rate
## [1] 0.4499541
Question 8
8a: n is the number of different observations in this study and p is the number of different predictors y = x - 2*x^2 + error
b: the scatter plot between x and y shows a normal distribution curve that is likely look more similar when n is larger
e: The second degree polynomial had the lowest standard error which does make sense considering the formula that we are predicting is also a 2nd degree polynomial
f: When looking at the Coefficients for each model we see a statistically significant correlation with the x and x^2 values which is consistent with trying to fit a second degree polynomial
## [1] 7.288162
## [1] 0.9374236
## [1] 0.9566218
## [1] 0.9539049
## [1] 7.899308
## [1] 1.158549
## [1] 1.18202
## [1] 1.190064
##
## Call:
## lm(formula = model_1, data = data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -10.3779 -1.1157 0.8074 1.7520 4.2401
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -1.7963 0.2715 -6.616 1.96e-09 ***
## x 0.9957 0.2765 3.601 5e-04 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 2.715 on 98 degrees of freedom
## Multiple R-squared: 0.1168, Adjusted R-squared: 0.1078
## F-statistic: 12.97 on 1 and 98 DF, p-value: 0.0005001
##
## Call:
## lm(formula = model_2, data = data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -2.47372 -0.63844 -0.07734 0.75610 2.82596
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.10881 0.13428 0.810 0.42
## x 1.07794 0.10843 9.941 <2e-16 ***
## I(x^2) -1.97569 0.08494 -23.260 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1.064 on 97 degrees of freedom
## Multiple R-squared: 0.8657, Adjusted R-squared: 0.863
## F-statistic: 312.7 on 2 and 97 DF, p-value: < 2.2e-16
##
## Call:
## lm(formula = model_3, data = data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -2.45779 -0.63432 -0.07761 0.77672 2.79549
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.10448 0.13552 0.771 0.443
## x 1.12888 0.18787 6.009 3.35e-08 ***
## I(x^2) -1.97014 0.08695 -22.659 < 2e-16 ***
## I(x^3) -0.01973 0.05928 -0.333 0.740
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1.069 on 96 degrees of freedom
## Multiple R-squared: 0.8659, Adjusted R-squared: 0.8617
## F-statistic: 206.6 on 3 and 96 DF, p-value: < 2.2e-16
##
## Call:
## lm(formula = model_4, data = data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -2.54874 -0.67502 -0.04766 0.80346 2.94651
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.22264 0.16305 1.366 0.175
## x 1.17850 0.19110 6.167 1.69e-08 ***
## I(x^2) -2.24812 0.23174 -9.701 7.19e-16 ***
## I(x^3) -0.04881 0.06320 -0.772 0.442
## I(x^4) 0.06045 0.04674 1.293 0.199
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1.065 on 95 degrees of freedom
## Multiple R-squared: 0.8682, Adjusted R-squared: 0.8627
## F-statistic: 156.5 on 4 and 95 DF, p-value: < 2.2e-16