Skip to content

Commit 6dc5b11

Browse files
author
Shimanto
authored
Add files via upload
1 parent fc81adb commit 6dc5b11

6 files changed

Lines changed: 193 additions & 0 deletions
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
install.packages('Ecdat')
2+
library(Ecdat)
3+
library(corrplot)
4+
library(caret)
5+
library(glmnet)
6+
data("VietNamI")
7+
str(VietNamI)
8+
p.cor<-cor(VietNamI[,-4])
9+
corrplot.mixed(p.cor)
10+
VietNamI$commune<-NULL
11+
VietNamI_reduced<-VietNamI[1:1000,]
12+
ind<-sample(2,nrow(VietNamI_reduced),replace=T,prob = c(0.7,0.3))
13+
train<-VietNamI_reduced[ind==1,]
14+
test<-VietNamI_reduced[ind==2,]
15+
grid<-expand.grid(.alpha=seq(0,1,by=.5),.lambda=seq(0,0.2,by=.1))
16+
control<-trainControl(method = "LOOCV")
17+
enet.train<-train(illdays~.,train,method="glmnet",trControl=control,tuneGrid=grid)
18+
enet.train
19+
20+
train$sex<-model.matrix( ~ sex - 1, data=train ) #convert to dummy variable
21+
test$sex<-model.matrix( ~ sex - 1, data=test )
22+
predictor_variables<-as.matrix(train[,-9])
23+
days_ill<-as.matrix(train$illdays)
24+
enet<-glmnet(predictor_variables,days_ill,family = "gaussian",alpha = 0.5,lambda = .2)
25+
26+
enet.coef<-coef(enet,lambda=.2,alpha=.5,exact=T)
27+
enet.coef
28+
29+
test.matrix<-as.matrix(test[,-9])
30+
enet.y<-predict(enet, newx = test.matrix, type = "response", lambda=.2,alpha=.5)
31+
32+
33+
plot(enet.y)
34+
35+
36+
enet.resid<-enet.y-test$illdays
37+
mean(enet.resid^2)
38+
39+
40+
set.seed(317)
41+
enet.cv<-cv.glmnet(predictor_variables,days_ill,alpha=.5)
42+
plot(enet.cv)
43+
44+
enet.cv$lambda.min
45+
enet.cv$lambda.1se
46+
47+
coef(enet.cv,s="lambda.1se")
48+
49+
50+
enet.y.cv<-predict(enet.cv,newx = test.matrix,type='response',lambda="lambda.1se", alpha = .5)
51+
enet.cv.resid<-enet.y.cv-test$illdays
52+
mean(enet.cv.resid^2)

Elastic Net Regression With Python & R/Elastic Net Regression with Python .ipynb

Lines changed: 141 additions & 0 deletions
Large diffs are not rendered by default.
48.5 KB
Loading
71.8 KB
Loading
77.7 KB
Loading
58.5 KB
Loading

0 commit comments

Comments
 (0)