# STA 414/2104, Spring 2006, Assignment #3. # # R. M. Neal # # Uses the natural linear spline functions from lspline.r. source("lspline.r") # Read the data to train and test on. x.train <- read.table("a3-x-train",head=F) y.train <- scan("a3-y-train") x.test <- read.table("a3-x-test",head=F) y.test <- scan("a3-y-test") x.grid <- read.table("a3-x-grid",head=F) # Fit with various values of lambda, and predict for the test cases. The # lambda with the smallest average squared prediction error is chosen as # the best one. lambda.values <- c (0.0001, 0.001, 0.003, 0.01, 0.02, 0.03, 0.05, 0.1, 0.2) best.ave.sq.err <- Inf for (i in 1:length(lambda.values)) { lambda <- lambda.values[i] beta <- lspline.fit (x.train, y.train, lambda) pred <- lspline.pred (x.train, beta, x.test) ave.sq.err <- mean((pred-y.test)^2) if (ave.sq.err