Copy of Final Assign.txt - install.packages"mboost...

Info icon This preview shows pages 1–4. Sign up to view the full content.

install.packages("mboost") library(class) library(ISLR) library(caret) library(gbm) library(rpart) library(MASS) library(tree) library(randomForest) library(boot) library(e1071) library(mboost) ##File_Path_To_Data## cdc=read.csv("FinalProjModData.csv") cdc$WEEK = as.factor(cdc$WEEK) CDC_DAt = data.frame(cdc) attach(CDC_DAt) fix(CDC_DAt) train <- subset(CDC_DAt, YEAR < 2015) test <- subset(CDC_DAt, YEAR >= 2015) ##Part1 1week #KNN set.seed(1) ctrl <- trainControl(method="repeatedcv", number = 10) KNN.Wk1 <- train(X1Step~WEEK+Pres.ILI+Last.ILI+Pres1wkRate+Last1wkRate+X4wkRate+Last4wkRate , data=train, method="knn", trControl = ctrl, preProcess = c("center","scale"), tuneLength = 20) KNN.Wk1 plot(KNN.Wk1) #Actual vs predicted res <- stack(data.frame(Observed = train$X1Step, Predicted = fitted(KNN.Wk1))) res <- cbind(res, x = rep(train$X1Step, 2)) head(res) require("lattice") xyplot(values ~ x, data = res, group = ind, auto.key = TRUE) set.seed(1) KNN.Wk2 <- train(X1Step~WEEK+Pres.ILI+Last.ILI+Pres1wkRate+Last1wkRate, data=train, method="knn", trControl = ctrl, preProcess = c("center","scale"), tuneLength = 20) KNN.Wk2 plot(KNN.Wk2) #Actual vs predicted res.1 <- stack(data.frame(Observed = train$X1Step, Predicted = fitted(KNN.Wk2))) res.1 <- cbind(res, x = rep(train$X1Step, 2)) head(res.1) require("lattice") xyplot(values ~ x, data = res, group = ind, auto.key = TRUE) set.seed(1) KNN.Wk3 <- train(X1Step~WEEK+Pres.ILI + Last.ILI +Pres1wkRate + Last1wkRate +X4wkRate + Last4wkRate +X4wkAvg,
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

data=train, method="knn", trControl = ctrl, preProcess = c("center","scale"), tuneLength = 20) KNN.Wk3 plot(KNN.Wk3) #Actual vs predicted res.1 <- stack(data.frame(Observed = train$X1Step, Predicted = fitted(KNN.Wk3))) res.1 <- cbind(res, x = rep(train$X1Step, 2)) head(res.1) require("lattice") xyplot(values ~ x, data = res, group = ind, auto.key = TRUE) ##Regression Tree ################################################################################ ##### #Method1 # grow tree set.seed(3) fit <- rpart(X1Step~Pres.ILI + Last.ILI +Pres1wkRate + Last1wkRate + X4wkRate + Last4wkRate + X4wkAvg, method="anova", data=train) printcp(fit) # display the results plotcp(fit) # visualize cross-validation results summary(fit) # detailed summary of splits # create additional plots par(mfrow=c(1,2)) # two plots on one page rsq.rpart(fit) # visualize cross-validation results # plot tree plot(fit, uniform=TRUE, main="Regression Tree for 1wk ") text(fit, use.n=TRUE, all=TRUE, cex=.7) pred.m1tree = predict(fit,test) mse.m1tree = mean((pred.m1tree - test$X1Step)^2) mse.m1tree ######################################## #Method 2 set.seed(1) CDC_Dat.tree.1<-tree(X1Step~Pres.ILI + Last.ILI +Pres1wkRate + Last1wkRate + X4wkRate + Last4wkRate + X4wkAvg, data=train) summary(CDC_Dat.tree.1) #unpruned plot(CDC_Dat.tree.1) text(CDC_Dat.tree.1, pretty = 0, cex=.7) ##calculate test MSE CDC_Dat.Pred=predict(CDC_Dat.tree.1, newdata = test) mean((CDC_Dat.Pred-test$X1Step)^2) #Cross Validate set.seed(1) cv.CDC_Dat = cv.tree(CDC_Dat.tree.1) names(cv.CDC_Dat) cv.CDC_Dat par(mfrow=c(1,2)) ##Size v dev
Image of page 2
##k v dev plot(cv.CDC_Dat$size, cv.CDC_Dat$dev, type = "b") plot(cv.CDC_Dat$k, cv.CDC_Dat$dev, type = "b") ## 7 is best but try 5 and 6 prune.CDC_Dat = prune(CDC_Dat.tree.1, best = 8) plot(prune.CDC_Dat, "Pruned Regression Tree") text(prune.CDC_Dat, all=TRUE, pretty=0, cex = .7) CDC_Dat.Pred.2=predict(prune.CDC_Dat, newdata=test) mean((CDC_Dat.Pred.2-test$X1Step)^2) #5 error of 10.8257 #6 error of 9.2098
Image of page 3

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern