Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
83 lines (72 sloc) 1.72 KB
#Clear workspace
rm(list=ls())
##Packages##
library(foreign)
library(xtable)
library(boot)
library(MASS)
library(ISLR)
library(e1071)
library(splines)
library(gbm)
library(randomForest)
library(glmnet)
library(glmpath)
library(LiblineaR)
library(rpart.plot)
library(rpart)
library(pROC)
library(ROCR)
library(Hmisc)
library(rattle)
library(cvAUC)
library(tree)
library(haven)
library(utils)
library(dummies)
##Importation des donnees##
cchs=read_dta("C:/Users/sboxc/Google Drive/Cedia/HUI/clean-cchs-2010-inv.dta")
myvars=names(cchs) %in% c("id","ltc")
cchs1=na.omit(cchs[!myvars])
cchs2=subset(cchs1, hc_hui<2)
##Separation de l'echantillon##
smp_size=floor(0.6 * nrow(cchs2))
set.seed(123)
train_ind=sample(seq_len(nrow(cchs2)), size = smp_size)
train=cchs2[train_ind, ]
test=cchs2[-train_ind, ]
##Arbre##
tree.fit=tree(hc_hui~.-wgt,data=train,weights=wgt, mindev=0.05)
plot(tree.fit); text(tree.fit)
print(tree.fit)
##Validation croisee et arbre emonde##
tree.cv=cv.tree(tree.fit, FUN=prune.tree, K=10)
plot(tree.cv)
print(tree.cv)
tree.pruned=prune.tree(tree.fit, best =98,method = c("deviance"))
plot(tree.pruned);text(tree.pruned)
yhat=predict(tree.pruned, test)
mse=mean((yhat - test$hc_hui)^2)
rmse=mse^.5
rmse
tree.pruned=prune.tree(tree.fit, best =2,method = c("deviance"))
plot(tree.pruned);text(tree.pruned)
yhat=predict(tree.pruned, test)
mse=mean((yhat - test$hc_hui)^2)
rmse=mse^.5
rmse
##Foret aleatoire##
rf=randomForest(hc_hui~.-wgt,data=train,weights=wgt)
varImpPlot(rf)
yhat.rf=predict(rf, test)
mse.rf=mean((yhat.rf - test$hc_hui)^2)
rmse.rf=mse.rf^.5
rmse.rf
rf
##OLS##
ols=lm(hc_hui~.-wgt,data=train,weights=wgt)
yhat.ols=predict(ols, test)
mse.ols=mean((yhat.ols - test$hc_hui)^2)
rmse.ols=mse.ols^.5
rmse.ols
summary(ols)