setwd("C:/Users/IBM/Desktop/研一课程/2.2回归分析/回归作业") #设定当前的工作目录
shuju=read.table("shuju.txt",header=T)
shuju #读取数据
#使用lm.ridge来进行岭回归,其好处是可通过GCV自动选择调节系数,但运算结果与书上不同
library(MASS) #lm.ridge在MASS包中
sx1<-scale(shuju$x1) #先标准化数据
sx2<-scale(shuju$x2)
sx3<-scale(shuju$x3)
sx4<-scale(shuju$x4)
sx5<-scale(shuju$x5)
sx6<-scale(shuju$x6)
sx7<-scale(shuju$x7)
sy<-scale(shuju$y)
shuju.x <- cbind(sx1,sx2,sx3,sx4,sx5,sx6,sx7)
la<-seq(0,1,0.05)
shuju.ridge<-lm.ridge(sy~sx1+sx2+sx3+sx4+sx5+sx6+sx7-1,lambda=la) #标准化数据要-1没截距项
coef(shuju.ridge)
#删除x4
fly.ridge1<-lm.ridge(sy~sx1+sx2+sx3+sx5+sx6+sx7-1,lambda=seq(0,1,0.001))#岭回归
#再删除x5
fly.ridge2<-lm.ridge(sy~sx1+sx2+sx3+sx6+sx7-1,lambda=seq(0,1,0.001))#岭回归
-------------------------------------------------------------------------------
setwd("C:/Users/IBM/Desktop/研一课程/2.2回归分析/回归作业") #设定当前的工作目录
shuju=read.table("shuju.txt",header=T)
shuju #读取数据
#使用glmnet函数求Lasso
library(glmnet)
sx1<-scale(shuju$x1) #先标准化数据
sx2<-scale(shuju$x2)
sx3<-scale(shuju$x3)
sx
shuju=read.table("shuju.txt",header=T)
shuju #读取数据
#使用lm.ridge来进行岭回归,其好处是可通过GCV自动选择调节系数,但运算结果与书上不同
library(MASS) #lm.ridge在MASS包中
sx1<-scale(shuju$x1) #先标准化数据
sx2<-scale(shuju$x2)
sx3<-scale(shuju$x3)
sx4<-scale(shuju$x4)
sx5<-scale(shuju$x5)
sx6<-scale(shuju$x6)
sx7<-scale(shuju$x7)
sy<-scale(shuju$y)
shuju.x <- cbind(sx1,sx2,sx3,sx4,sx5,sx6,sx7)
la<-seq(0,1,0.05)
shuju.ridge<-lm.ridge(sy~sx1+sx2+sx3+sx4+sx5+sx6+sx7-1,lambda=la) #标准化数据要-1没截距项
coef(shuju.ridge)
#删除x4
fly.ridge1<-lm.ridge(sy~sx1+sx2+sx3+sx5+sx6+sx7-1,lambda=seq(0,1,0.001))#岭回归
#再删除x5
fly.ridge2<-lm.ridge(sy~sx1+sx2+sx3+sx6+sx7-1,lambda=seq(0,1,0.001))#岭回归
-------------------------------------------------------------------------------
setwd("C:/Users/IBM/Desktop/研一课程/2.2回归分析/回归作业") #设定当前的工作目录
shuju=read.table("shuju.txt",header=T)
shuju #读取数据
#使用glmnet函数求Lasso
library(glmnet)
sx1<-scale(shuju$x1) #先标准化数据
sx2<-scale(shuju$x2)
sx3<-scale(shuju$x3)
sx