Chapter 9 Exercise 6

Sign in to test your solution.
set.seed(1) x.one <- runif(500, 0, 90) y.one <- runif(500, x.one + 10, 100) x.one.noise <- runif(50, 20, 80) y.one.noise <- 5/4 * (x.one.noise - 10) + 0.1 x.zero <- runif(500, 10, 100) y.zero <- runif(500, 0, x.zero - 10) x.zero.noise <- runif(50, 20, 80) y.zero.noise <- 5/4 * (x.zero.noise - 10) - 0.1 class.one <- seq(1, 550) x <- c(x.one, x.one.noise, x.zero, x.zero.noise) y <- c(y.one, y.one.noise, y.zero, y.zero.noise) plot(x[class.one], y[class.one], col = "blue", pch = "+", ylim = c(0, 100)) points(x[-class.one], y[-class.one], col = "red", pch = 4) z <- rep(0, 1100) z[class.one] <- 1 data <- data.frame(x = x, y = y, z = as.factor(z)) # 1 cross-validation set.seed(2) # 2 create dataframe # 3 compute test errors set.seed(3) x.test <- runif(1000, 0, 100) class.one <- sample(1000, 500) y.test <- rep(NA, 1000) # Set y > x for class.one for (i in class.one) { y.test[i] <- runif(1, x.test[i], 100) } # set y < x for class.zero for (i in setdiff(1:1000, class.one)) { y.test[i] <- runif(1, 0, x.test[i]) } plot(x.test[class.one], y.test[class.one], col = "blue", pch = "+") points(x.test[-class.one], y.test[-class.one], col = "red", pch = 4) z.test <- rep(0, 1000) z.test[class.one] <- 1 data.test <- data.frame(x = x.test, y = y.test, z = as.factor(z.test)) costs <- c(0.01, 0.1, 1, 5, 10, 100, 1000, 10000)
You can submit as many times as you like. Only your latest submission will be taken into account.
Sign in to test your solution.