参考以下两段代码代码:第一段:# Lab5: Cross-Validation and the Bootstrap # The Validation Set Approach install.packages("ISLR") library(ISLR) set.seed(1) train=sample(392,196) lm.fit=lm(mpg~horsepower,data=Auto,subset=train) attach(Auto) mean((mpg-predict(lm.fit,Auto))[-train]^2) lm.fit2=lm(mpg~poly(horsepower,2),data=Auto,subset=train) mean((mpg-predict(lm.fit2,Auto))[-train]^2) lm.fit3=lm(mpg~poly(horsepower,3),data=Auto,subset=train) mean((mpg-predict(lm.fit3,Auto))[-train]^2) set.seed(2) train=sample(392,196) lm.fit=lm(mpg~horsepower,subset=train) mean((mpg-predict(lm.fit,Auto))[-train]^2) lm.fit2=lm(mpg~poly(horsepower,2),data=Auto,subset=train) mean((mpg-predict(lm.fit2,Auto))[-train]^2) lm.fit3=lm(mpg~poly(horsepower,3),data=Auto,subset=train) mean((mpg-predict(lm.fit3,Auto))[-train]^2) # Leave-One-Out Cross-Validation glm.fit=glm(mpg~horsepower,data=Auto) coef(glm.fit) lm.fit=lm(mpg~horsepower,data=Auto) coef(lm.fit) library(boot) glm.fit=glm(mpg~horsepower,data=Auto) cv.err=cv.glm(Auto,glm.fit) cv.err$delta cv.error=rep(0,5) for (i in 1:5){ glm.fit=glm(mpg~poly(horsepower,i),data=Auto) cv.error[i]=cv.glm(Auto,glm.fit)$delta[1] } cv.error第二段:library(caret) library(klaR) data(iris) splt=0.80 trainIndex <- createDataPartition(iris$Species,p=split,list=FALSE) data_train <- iris[ trainIndex,] data_test <- iris[-trainIndex,] model <- NaiveBayes(Species~.,data=data_train) x_test <- data_test[,1:4] y_test <- data_test[,5] predictions <- predict(model,x_test) confusionMatrix(predictions$class,y_test)。写出R代码完成以下任务:①建立50×30的随机数据和30个变量;②生成三组不同系数的①线性模型;③(线性回归中)分别计算这三组的CV值;④(岭回归中)分别画出这三组的两张图,两张图均以lambd为横坐标,一张图以CV error为纵坐标,一张图以Prediction error为纵坐标,两张图同分开在Plots位置,而且三组一组画完,再画另一组
时间: 2024-02-22 15:59:12 浏览: 127
jakarta.validation-api-2.0.2-API文档-中文版.zip
5星 · 资源好评率100%
以下是R代码:
①
```R
set.seed(1)
data <- matrix(rnorm(1500), nrow = 50, ncol = 30)
```
②
```R
# 第一组线性模型
lm.fit1 <- lm(data[,1] ~ data[,2]+data[,3]+data[,4]+data[,5]+data[,6]+
data[,7]+data[,8]+data[,9]+data[,10]+data[,11]+data[,12]+
data[,13]+data[,14]+data[,15]+data[,16]+data[,17]+data[,18]+
data[,19]+data[,20]+data[,21]+data[,22]+data[,23]+data[,24]+
data[,25]+data[,26]+data[,27]+data[,28]+data[,29]+data[,30])
# 第二组线性模型
lm.fit2 <- lm(data[,1] ~ data[,2]+data[,3]+data[,4]+data[,5]+data[,6]+
data[,7]+data[,8]+data[,9]+data[,10]+data[,11]+data[,12]+
data[,13]+data[,14]+data[,15]+data[,16]+data[,17]+data[,18]+
data[,19]+data[,20]+data[,21]+data[,22]+data[,23]+data[,24]+
data[,25]+data[,26]+data[,27]+data[,28]+data[,29]+data[,30])
# 第三组线性模型
lm.fit3 <- lm(data[,1] ~ data[,2]+data[,3]+data[,4]+data[,5]+data[,6]+
data[,7]+data[,8]+data[,9]+data[,10]+data[,11]+data[,12]+
data[,13]+data[,14]+data[,15]+data[,16]+data[,17]+data[,18]+
data[,19]+data[,20]+data[,21]+data[,22]+data[,23]+data[,24]+
data[,25]+data[,26]+data[,27]+data[,28]+data[,29]+data[,30])
```
③
```R
# 第一组线性模型
library(boot)
cv.fit1 <- cv.glm(data, lm.fit1)
cv.fit1$delta[1]
# 第二组线性模型
cv.fit2 <- cv.glm(data, lm.fit2)
cv.fit2$delta[1]
# 第三组线性模型
cv.fit3 <- cv.glm(data, lm.fit3)
cv.fit3$delta[1]
```
④
```R
library(glmnet)
# 第一组岭回归
set.seed(1)
cv.ridge1 <- cv.glmnet(data[,2:30], data[,1], alpha = 0)
lambda.min1 <- cv.ridge1$lambda.min
ridge.fit1 <- glmnet(data[,2:30], data[,1], alpha = 0)
plot(ridge.fit1, xvar = "lambda", label = TRUE)
abline(v = lambda.min1, lty = "dashed", lwd = 1, col = "red")
ridge.pred1 <- predict(ridge.fit1, newx = data[,2:30], s = lambda.min1)
ridge.cv.error1 <- mean((ridge.pred1 - data[,1])^2)
ridge.cv.error1
# 第二组岭回归
set.seed(1)
cv.ridge2 <- cv.glmnet(data[,2:30], data[,1], alpha = 0.5)
lambda.min2 <- cv.ridge2$lambda.min
ridge.fit2 <- glmnet(data[,2:30], data[,1], alpha = 0.5)
plot(ridge.fit2, xvar = "lambda", label = TRUE)
abline(v = lambda.min2, lty = "dashed", lwd = 1, col = "red")
ridge.pred2 <- predict(ridge.fit2, newx = data[,2:30], s = lambda.min2)
ridge.cv.error2 <- mean((ridge.pred2 - data[,1])^2)
ridge.cv.error2
# 第三组岭回归
set.seed(1)
cv.ridge3 <- cv.glmnet(data[,2:30], data[,1], alpha = 1)
lambda.min3 <- cv.ridge3$lambda.min
ridge.fit3 <- glmnet(data[,2:30], data[,1], alpha = 1)
plot(ridge.fit3, xvar = "lambda", label = TRUE)
abline(v = lambda.min3, lty = "dashed", lwd = 1, col = "red")
ridge.pred3 <- predict(ridge.fit3, newx = data[,2:30], s = lambda.min3)
ridge.cv.error3 <- mean((ridge.pred3 - data[,1])^2)
ridge.cv.error3
```
阅读全文