능형

Published by onesixx on

 

#################################################################
###Step 1: Data 
library(ISLR)
df <- read.csv("http://www-bcf.usc.edu/~gareth/ISL/Credit.csv", header=T, sep=",")
#sum(is.na(df$Income))   # Y의 누락값 확인 
str(df); head(df)


x <- model.matrix(Balance~., df)[,-1]    # 절편(Intercept) 제외 
y <- df$Balance
dim(x); length(y)  # 263  19 , 263

#install.packages("glmnet")
library(glmnet)

grid <- 10^seq(10, -2, length=100)
ridge.mod <- glmnet(x, y, alpha=0, lambda=grid) # alpha=0이면, ridge regression model로 fitting

names(ridge.mod)      

ridge.mod$dim           # 19 100
dim(coef(ridge.mod))    # 20 100

ridge.mod$lambda[50]
coef(ridge.mod)[,50]
sqrt( sum( coef(ridge.mod)[-1,50]^2 ) ) 

ridge.mod$lambda[60]
coef(ridge.mod)[,60]
sqrt( sum( coef(ridge.mod)[-1,60]^2 ) ) 

predict(ridge.mod, s=50, type="coefficients")[1:20, ]

gdf <- rbind(
    data.frame(num=c(100:1),nvar=ridge.mod$lambda, var=rep("Income", 100), value=coef(ridge.mod)[3,])
    ,data.frame(num=c(100:1),nvar=c(100:1), var=rep("Limit", 100), value=coef(ridge.mod)[4,])
    ,data.frame(num=c(100:1),nvar=c(100:1), var=rep("Rating", 100), value=coef(ridge.mod)[5,])
    #,data.frame(num=c(100:1),nvar=c(100:1), var=rep("StudentYes", 100), value=coef(ridge.mod)[10,]  )
)

ggplot( gdf, aes(x=num, y=value, color=var)) +
geom_path()

 

 

Categories: book:ISLR

onesixx

Blog Owner

Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x