library(MASS)
library(caret)
# createDataPartition
set.seed(123)
train <- createDataPartition(y=Boston$medv, p=0.7, list=F)
head(train)
Boston.train <- Boston[train,]
Boston.test <- Boston[-train,]
#glmnet elasticnet : alpha = 0.1 lambda = 0.08747614.
set.seed(123)
Boston.gnet <- train(form=medv ~ ., data=Boston.train, method="glmnet",
trControl=trainControl(method="cv",number=10), tuneLength=10)
#rf 오래걸림 : mtry = 5
Boston.rf <- train(form=medv ~ ., data=Boston.train, method="rf",
trControl=trainControl(method="cv",number=10), tuneLength=10)
#svm : sigma = 0.04434552 and C = 32.
Boston.svm <- train(form=medv ~ ., data=Boston.train, method="svmRadialSigma",
trControl=trainControl(method="cv",number=10), tuneLength=10)
# predict
gnet.pred <- predict(Boston.gnet,Boston.test)
postResample(pred=gnet.pred, obs=Boston.test$medv)
rf.pred <- predict(Boston.rf,Boston.test)
postResample(pred=rf.pred, obs=Boston.test$medv)
svm.pred <- predict(Boston.svm,Boston.test)
postResample(pred=svm.pred, obs=Boston.test$medv)
#comparison
models <- list(gnet=Boston.gnet, rf=Boston.rf, svm=Boston.svm)
summary(resamples(models))
summary(resamples(models), metric="RMSE")
summary(diff(resamples(models), metric="RMSE"))
'ADP (R)' 카테고리의 다른 글
[R] plot las=1 (2) | 2022.09.19 |
---|---|
[R] plot type="b" (2) | 2022.09.19 |
[Adp 실기 기출 풀이] 22회 구매패턴 연관성 (0) | 2022.09.14 |
[R] x is not a factor 오류 해결 (0) | 2022.09.14 |
R document 에 없는 매개변수? (plot 함수의 hang) (0) | 2022.09.03 |