Skip to content
Snippets Groups Projects
Commit b26def73 authored by Felix Ramnelöv's avatar Felix Ramnelöv
Browse files

Lab 2: Fixed error in assignment 3

parent 1e8650a4
No related branches found
No related tags found
No related merge requests found
......@@ -5,14 +5,14 @@ data = read.csv("communities.csv")
# ----1.----
columns_to_scale <- setdiff(names(data), "ViolentCrimesPerPop")
feature_column_names <- setdiff(names(data), "ViolentCrimesPerPop")
scaler <- preProcess(data[, columns_to_scale])
scaler <- preProcess(data[, feature_column_names])
data_scaled <- data
data_scaled[, columns_to_scale] <- predict(scaler, data[, columns_to_scale])
data_scaled[, feature_column_names] <- predict(scaler, data[, feature_column_names])
X <- data_scaled[, columns_to_scale]
X <- data_scaled[, feature_column_names]
y <- data_scaled$ViolentCrimesPerPop
covariance_matrix <- cov(X)
......@@ -65,18 +65,17 @@ id = sample(1:n, floor(n * 0.5))
train = data[id, ]
test = data[-id, ]
scaler3 <- preProcess(train[, columns_to_scale])
scaler3 <- preProcess(train)
train_scaled <- train
train_scaled[, columns_to_scale] <- predict(scaler3, train[, columns_to_scale])
train_scaled<- predict(scaler3, train)
test_scaled <- test
test_scaled[, columns_to_scale] <- predict(scaler3, test[, columns_to_scale])
test_scaled <- predict(scaler3, test)
X_train <- train_scaled[, columns_to_scale]
X_train <- train_scaled[, feature_column_names]
y_train <- train_scaled$ViolentCrimesPerPop
X_test <- test_scaled[, columns_to_scale]
X_test <- test_scaled[, feature_column_names]
y_test <- test_scaled$ViolentCrimesPerPop
model <- lm(ViolentCrimesPerPop ~ . - 1, data = train_scaled)
......@@ -102,8 +101,8 @@ cost_function <- function(theta) {
train_error <- MSE(y_train, as.matrix(X_train) %*% theta)
test_error <- MSE(y_test, as.matrix(X_test) %*% theta)
.GlobalEnv$train_errors <- c(train_errors, train_error)
.GlobalEnv$test_errors <- c(test_errors, test_error)
train_errors <<- c(train_errors, train_error)
test_errors <<- c(test_errors, test_error)
return(train_error)
}
......@@ -112,7 +111,7 @@ res <- optim(par = rep(0, ncol(X_train)),
fn = cost_function,
method = "BFGS",)
range <- c(train_errors[500:length(train_errors)], test_errors[500:length(test_errors)])
range <- c(train_errors[501:length(train_errors)], test_errors[501:length(test_errors)])
plot(
train_errors,
......@@ -122,7 +121,8 @@ plot(
xlab = "Iteration",
main = "Training and Test Errors over Iterations",
lty = 1,
ylim = c(min(range), max(range))
ylim = c(min(range), max(range)),
xlim = c(501,length(train_errors)),
)
points(test_errors,
......@@ -137,7 +137,7 @@ legend(
lty = c(1, 2)
)
early_stopping <- which.min(test_errors[500:length(test_errors)]) + 500
early_stopping <- which.min(test_errors)
early_stopping
test_errors[early_stopping]
......@@ -147,4 +147,4 @@ optimal_train_mse <- res$value
optimal_train_mse
optimal_test_mse <- MSE(y_test, as.matrix(X_test) %*% optimal_theta)
optimal_test_mse
optimal_test_mse
\ No newline at end of file
This diff is collapsed.
lab2/figures/assignment3-4.png

4.69 KiB | W: | H:

lab2/figures/assignment3-4.png

4.75 KiB | W: | H:

lab2/figures/assignment3-4.png
lab2/figures/assignment3-4.png
lab2/figures/assignment3-4.png
lab2/figures/assignment3-4.png
  • 2-up
  • Swipe
  • Onion skin
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment