Load the Alzheimer’s disease data using the commands:
library(AppliedPredictiveModeling); library(caret)
data(AlzheimerDisease)
Which of the following commands will create non-overlapping training and test sets with about 50% of the observations assigned to each?
adData = data.frame(diagnosis,predictors)
testIndex = createDataPartition(diagnosis, p = 0.50,list=FALSE)
training = adData[-testIndex,]
testing = adData[testIndex,]
Load the cement data using the commands:
library(AppliedPredictiveModeling)
data(concrete)
library(caret)
set.seed(1000)
inTrain = createDataPartition(mixtures$CompressiveStrength, p = 3/4)[[1]]
training = mixtures[ inTrain,]
testing = mixtures[-inTrain,]
Make a plot of the outcome (CompressiveStrength) versus the index of the samples. Color by each of the variables in the data set (you may find the cut2() function in the Hmisc package useful for turning continuous covariates into factors). What do you notice in these plots?
qplot(CompressiveStrength, row.names(training), colour=Age, data=training)
library(Hmisc)
cutAge<-cut2(training$Age, g=4)
qplot(CompressiveStrength, row.names(training), colour=cutAge, data=training)
There is a non-random pattern in the plot of the outcome versus index that does not appear to be perfectly explained by any predictor suggesting a variable may be missing.
Load the cement data using the commands:
library(AppliedPredictiveModeling)
data(concrete)
library(caret)
set.seed(1000)
inTrain = createDataPartition(mixtures$CompressiveStrength, p = 3/4)[[1]]
training = mixtures[ inTrain,]
testing = mixtures[-inTrain,]
Make a histogram and confirm the SuperPlasticizer variable is skewed. Normally you might use the log transform to try to make the data more symmetric. Why would that be a poor choice for this variable?
hist(training$Superplasticizer)
summary(training$Superplasticizer)
hist(log10(training$Superplasticizer+1))
There are values of zero so when you take the log() transformation, those values will be -inf.
Load the Alzheimer’s disease data using the commands:
library(caret); library(AppliedPredictiveModeling)
set.seed(3433); data(AlzheimerDisease)
adData = data.frame(diagnosis,predictors)
inTrain = createDataPartition(adData$diagnosis, p = 3/4)[[1]]
training = adData[ inTrain,]
testing = adData[-inTrain,]
Find all the predictor variables in the training set that begin with IL. Perform principal components on these variables with the preProcess() function from the caret package. Calculate the number of principal components needed to capture 90% of the variance. How many are there?
trainingIL<-training[, c(grep("^IL", names(training)), 1)]
testingIL<-testing[, c(grep("^IL", names(testing)), 1)]
preProc<-preProcess(trainingIL[, -13], method="pca", thresh = 0.9)
Create a training data set consisting of only the predictors with variable names beginning with IL and the diagnosis. Build two predictive models, one using the predictors as they are and one using PCA with principal components explaining 80% of the variance in the predictors. Use method=“glm” in the train function.
What is the accuracy of each method in the test set? Which is more accurate?
trainingIL<-training[, c(grep("^IL", names(training)), 1)]
testingIL<-testing[, c(grep("^IL", names(testing)), 1)]
# Model that uses all the predictors as they are
mdfit1<-train(diagnosis~., method="glm", data=trainingIL)
confusionMatrix(testing$diagnosis, predict(mdfit1, testing))
# Model that uses PCA explaining 80% of variance
preProc<-preProcess(trainingIL[, -13], method="pca", thresh = 0.8)
trainPC<-predict(preProc, trainingIL[, -13])
mdfit2<-train(x=trainPC, y=trainingIL$diagnosis, method="glm")
testPC<-predict(preProc, testingIL[, -13])
confusionMatrix(testingIL$diagnosis, predict(mdfit2, testPC))
# The below method for the PCA model yields similar results
#mdfit2<-train(diagnosis~., method="glm", preProcess="pca", data=trainingIL)
#confusionMatrix(testingIL$diagnosis, predict(mdfit2, testingIL))
PCA model is more accurate.
Copyright © 2019 Cathy Gao at cathygao.2019@outlook.com.