class=”markdown_views prism-atom-one-dark”>
I have been using Python/R/Shell for background development, but lack of communication. It’s the first time I try to post what I’m learning on the blog, hoping to get some guidance from the experts so that I can get some sunshine.
library(AppliedPredictiveModeling)
data(concrete)
library(caret)
library(plyr)
head(concrete,2)
Cement | BlastFurnaceSlag | FlyAsh | Water | Superplasticizer | CoarseAggregate | FineAggregate | Age | CompressiveStrength |
---|---|---|---|---|---|---|---|---|
540 | 0 | 0 | 162 | 2.5 | 1040 | 676 | 28 | 79.99 |
540 | 0 | 0 | 162 | 2.5 | 1055 | 676 | 28 | 61.89 |
featurePlot(concrete[, -9], concrete$CompressiveStrength,
between = list(x = 1, y = 1),
type = c("g", "p", "smooth"))
averaged <- ddply(mixtures,
.(Cement, BlastFurnaceSlag, FlyAsh, Water,
Superplasticizer, Coarse Aggregate,
FineAggregate, Age),
function(x) c(CompressiveStrength =
mean(x$CompressiveStrength)))
head(mixtures,2)
Cement | BlastFurnaceSlag | FlyAsh | Water | Superplasticizer | CoarseAggregate | FineAggregate | Age | CompressiveStrength |
---|---|---|---|---|---|---|---|---|
0.2230944 | 0 | 0 | 0.06692832 | 0.001032844 | 0.4296633 | 0.2792811 | 28 | 79.99 |
0.2217204 | 0 | 0 | 0.06651612 | 0.001026483 | 0.4331759 | 0.2775611 | 28 | 61.89 |
set.seed(975)
inTrain <- createDataPartition(averaged$CompressiveStrength, p = 3/4)[[1]]
training <- averaged[inTrain,]
testing <- averaged[-inTrain,]
### Repeated Training/Test Splits
ctrl <- trainControl(method = "repeatedcv", repeats = 5, number span> = 10)
### Create a model formula that can be used repeatedly
modForm <- paste("CompressiveStrength ~ (.)^2 + I(Cement^2) + I(BlastFurnaceSlag^2) +",
"I(FlyAsh^2) + I(Water^2) + I(Superplasticizer^2) + ",
"I(CoarseAggregate^2) + I(FineAggregate^2) + I(Age^2)" )
modForm <- as.formula(- hljs-attribute">modForm)
### lm & pls: Partial Least Squares
set.seed(669)
lmFit <- train(modForm, data = training,
method = "lm",
trControl = ctrl)
set.seed(669)
plsFit <- train(modForm, data = training,
method = "pls",
preProc = c("center", "scale"),
tuneLength = 15,
trControl = ctrl)
Attaching package: 'pls'
The following object is masked from 'package:caret':
R2
The following object is masked from 'package:stats':
loads
### ridge regression
lassoGrid <- expand.grid(lambda = c(0, .001, .01, .1),
fraction = seq(0.05, 1, length = 20))
set.seed(669)
lassoFit <- train(modForm, data = training,
method = "enet",
preProc = c("center", "scale") ,
tuneGrid = lassoGrid,
trControl = ctrl)
Loading required package: elasticnet
### Multivariate Adaptive Regression Splines
set.seed(669)
earthFit <- train(CompressiveStrength ~ ., data = training,
method = "earth",
tuneGrid = expand.grid(degree = 1,
nprune = 2:25),
trControl = ctrl)
Loading required package: earth
Loading required package: plotmo
Loading required package: plotrix
Loading required package: TeachingDemos
Warning message:
"package 'TeachingDemos' was built under R version 3.2.5"
### SVM
set.seed(669)
svmRFit <- train(CompressiveStrength ~ ., data = training,
method = "svmRadial",
tuneLength = 15,
preProc = c("center", "scale") ,
trControl = ctrl)
Loading required package: kernlab
Attaching package: 'kernlab'
The following object is masked from 'package:ggplot2':
alpha
### neural network models
### don't run it due that it'll take more times!
nnetGrid <- expand.grid(decay = c(0.001, .01, .1),
size = seq(1, 27, by = 2),
bag = FALSE)
set.seed(669)
nnetFit <- train(CompressiveStrength ~ .,
data = training,
method = "avNNet",
tuneGrid = nnetGrid,
preProc = c("center", "scale") ,
linout = TRUE,
trace = FALSE,
maxit = 50,
allowParallel = FALSE,
trControl = ctrl)
Loading required package: nnet
set.seed(669)
rpartFit <- train(CompressiveStrength ~ .,
data = training,
method = "rpart",
tuneLength = 30,
trControl = ctrl)
Loading required package: rpart
set.seed(669)
treebagFit <- train(CompressiveStrength ~ .,
data = training,
method = "treebag",
trControl = ctrl)
Loading required package: ipred
Warning message:
"package 'ipred' was built under R version 3.2.5"Loading required package: e1071
set.seed(669)
ctreeFit <- train(CompressiveStrength ~ .,
data = training,
method = "ctree",
tuneLength = 10,
trControl = ctrl)
Loading required package: party
Warning message:
"package 'party' was built under R version 3.2.5"Loading required package: grid
Loading required package: mvtnorm
Warning message:
"package 'mvtnorm' was built under R version 3.3.0"Loading required package: modeltools
Warning message:
"package 'modeltools' was built under R version 3.2.5"Loading required package: stats4
Attaching package: 'modeltools'
The following object is masked from 'package:kernlab':
prior
The following object is masked from 'package:plyr':
empty
Loading required package: strucchange
Warning message:
"package 'strucchange' was built under R version 3.2.5"Loading required package: zoo
Attaching package: 'zoo'
The following objects are masked from 'package:base':
as.Date, as.Dat
pan>=15,
preProc = c(“center”, “scale”) ,
trControl = ctrl)
Loading required package: kernlab
Attaching package: 'kernlab'
The following object is masked from 'package:ggplot2':
alpha
### neural network models
### don't run it due that it'll take more times!
nnetGrid <- expand.grid(decay = c(0.001, .01, .1),
size = seq(1, 27, by = 2),
bag = FALSE)
set.seed(669)
nnetFit <- train(CompressiveStrength ~ .,
data = training,
method = "avNNet",
tuneGrid = nnetGrid,
preProc = c("center", "scale") ,
linout = TRUE,
trace = FALSE,
maxit = 50,
allowParallel = FALSE,
trControl = ctrl)
Loading required package: nnet
set.seed(669)
rpartFit <- train(CompressiveStrength ~ .,
data = training,
method = "rpart",
tuneLength = 30,
trControl = ctrl)
Loading required package: rpart
set.seed(669)
treebagFit <- train(CompressiveStrength ~ .,
data = training,
method = "treebag",
trControl = ctrl)
Loading required package: ipred
Warning message:
"package 'ipred' was built under R version 3.2.5"Loading required package: e1071
set.seed(669)
ctreeFit <- train(CompressiveStrength ~ .,
data = training,
method = "ctree",
tuneLength = 10,
trControl = ctrl)
Loading required package: party
Warning message:
"package 'party' was built under R version 3.2.5"Loading required package: grid
Loading required package: mvtnorm
Warning message:
"package 'mvtnorm' was built under R version 3.3.0"Loading required package: modeltools
Warning message:
"package 'modeltools' was built under R version 3.2.5"Loading required package: stats4
Attaching package: 'modeltools'
The following object is masked from 'package:kernlab':
prior
The following object is masked from 'package:plyr':
empty
Loading required package: strucchange
Warning message:
"package 'strucchange' was built under R version 3.2.5"Loading required package: zoo
Attaching package: 'zoo'
The following objects are masked from 'package:base':
as.Date, as.Dat