R语言 偏最小二乘回归PLS代码

导入数据集,标准化变量

library(pls)
#读取数据赋值
df = read.csv("soil.csv")
y = scale(df$soil.som) #标准化
x = scale(df[,2:50])   #标准化
soil.plsr = plsr(y~x,validation = "CV") #使用交叉验证确定主成分
summary(soil.plsr)
plot(RMSEP(soil.plsr),legend = "topright")#根据RMSEP确定主成分个数
soil.plsr2  = plsr(y~x,ncomp = 8,validation = "CV") #选择8个成分,重新建立模型
summary(soil.plsr2)

pre_y = predict(soil.plsr2,ncomp = 8,newdata = x) #获得预测值

mse = sum((y-pre_y[,,1])**2/length(y)) #计算mse
R2 = 1-mse #R方
mae = sum(abs(y-pre_y[,,1])/length(y))#mae

R语言 偏最小二乘回归PLS代码_第1张图片

 

你可能感兴趣的:(r语言,回归)