Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions Rmarkdown/Cross-ValidationInClassHO.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ The left side of Figure 5.2 on page 178 of @james_introduction_2013 shows the va
```{r}
library(ISLR)
n <- nrow(Auto)
plot(1:10, type ="n", xlab = "Degree of Polynomial", ylim = c(15, 30),
plot(1:10, type = "n", xlab = "Degree of Polynomial", ylim = c(15, 30),
ylab = "Mean Squared Prediction Error")
IND <- sample(1:n, size = floor(n/2), replace = FALSE)
train <- Auto[IND, ]
Expand Down Expand Up @@ -277,11 +277,11 @@ table(folds)
sum(xtabs(~folds))
for(j in 1:k){
modq <- lm(y ~ poly(x, 2, raw = TRUE), data = DF[folds != j, ])
pred <- predict(modq, newdata = DF[folds ==j, ])
pred <- predict(modq, newdata = DF[folds == j, ])
MSPE[j] <- mean((DF[folds == j, ]$y - pred)^2)
}
MSPE
weighted.mean(MSPE, table(folds)/sum(folds))
weighted.mean(MSPE, w = table(folds)/sum(xtabs(~folds)))
```

### Using `caret`
Expand Down Expand Up @@ -382,7 +382,7 @@ The right side of Figure 5.4 on page 180 of @james_introduction_2013 shows the 1

```{r}
# Your code here
plot(1:10, type ="n", xlab = "Degree of Polynomial", ylim = c(16, 26),
plot(1:10, type = "n", xlab = "Degree of Polynomial", ylim = c(16, 26),
ylab = "Mean Squared Prediction Error", main = "10-fold CV")
k <- 10 # number of folds
MSPE <- numeric(k)
Expand All @@ -406,7 +406,7 @@ cv <- numeric(k)
```{r}
# Your Code Here
set.seed(123)
plot(1:10, type ="n", xlab = "Degree of Polynomial", ylim = c(16, 26),
plot(1:10, type = "n", xlab = "Degree of Polynomial", ylim = c(16, 26),
ylab = "Mean Squared Prediction Error", main = "10-fold CV")
k <- 10 # number of folds
MSPE <- numeric(k)
Expand Down Expand Up @@ -455,7 +455,7 @@ MSPE <- numeric(k)

```{r}
# Your Code Here
plot(1:10, type ="n", xlab = "Degree of Polynomial", ylim = c(16, 26),
plot(1:10, type = "n", xlab = "Degree of Polynomial", ylim = c(16, 26),
ylab = "Mean Squared Prediction Error", main = "10-fold CV")
#
#
Expand Down Expand Up @@ -577,7 +577,7 @@ glm.fit <- glm(hwfat ~ abs + triceps, data = HSWRESTLER)

```{r}
# Your Code Here
plot(1:10, type ="n", xlab = "Degree of Polynomial", ylim = c(16, 26),
plot(1:10, type = "n", xlab = "Degree of Polynomial", ylim = c(16, 26),
ylab = "Mean Squared Prediction Error")
k <- nrow(Auto) # number of folds
MSPE <- numeric(k)
Expand All @@ -600,7 +600,7 @@ Using the short cut formula:

```{r}
# Your Code Here
plot(1:10, type ="n", xlab = "Degree of Polynomial", ylim = c(16, 26),
plot(1:10, type = "n", xlab = "Degree of Polynomial", ylim = c(16, 26),
ylab = "Mean Squared Prediction Error")
cv <- numeric(10)
#
Expand Down