# 능형

```#################################################################
###Step 1: Data
library(ISLR)
#sum(is.na(df\$Income))   # Y의 누락값 확인

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

Blog Owner

Subscribe
Notify of