ok - sidewallme/Graph-Theory GitHub Wiki


title: "win" output: html_document

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:


library(mlbench)
library(caret)
library(data.table)
library(e1071)

download.file("https://raw.githubusercontent.com/sidewallme/2015-Rose-Hulman-Competition/master/train.csv", method = 'curl', destfile = 'train.csv')
train <- fread("train.csv")
download.file("https://raw.githubusercontent.com/sidewallme/2015-Rose-Hulman-Competition/master/test.csv", method = 'curl', destfile = 'test.csv')
test <- fread("test.csv")


You can also embed plots, for example:

training <- train[sample(nrow(train)),]
training$YearDiff <- training$CalendarYear - training$ModelYear
training$OrdCat <- as.factor(training$OrdCat)
training$NVCat <- as.factor(training$NVCat)
training$RowID <- NULL
training$Model <- NULL
training$ModelYear <- NULL
training$CalendarYear <- NULL

training$Class <- training$Claim
training$Claim <- NULL
testing <- test
testing$YearDiff <- testing$CalendarYear - testing$ModelYear
testing$OrdCat <- as.factor(testing$OrdCat)
testing$NVCat <- as.factor(testing$NVCat)
testing$RowID <- NULL
testing$Model <- NULL
testing$ModelYear <- NULL
testing$CalendarYear <- NULL
#No. 35
'''
 [1] "Make"     "Cat1"     "Cat2"     "Cat3"     "Cat4"     "Cat5"     "Cat6"     "Cat7"     "Cat8"    
[10] "Cat9"     "Cat10"    "Cat11"    "Cat12"    "OrdCat"   "Var1"     "Var2"     "Var3"     "Var4"    
[19] "Var5"     "Var6"     "Var7"     "Var8"     "NVCat"    "NVVar1"   "NVVar2"   "NVVar3"   "NVVar4"  
[28] "YearDiff" "Class"   
'''
temp <- training
temp <- as.data.frame(temp)
temp <- cbind(temp[1:14],temp[23],temp[15:22],temp[24:29])
names <- colnames(temp)

temp <- transform(temp, MakeWITHCat1=paste(Make,Cat1))
temp <- transform(temp, MakeWITHCat2=paste(Make,Cat2))
temp <- transform(temp, MakeWITHCat3=paste(Make,Cat3))
temp <- transform(temp, MakeWITHCat4=paste(Make,Cat4))
temp <- transform(temp, MakeWITHCat5=paste(Make,Cat5))
temp <- transform(temp, MakeWITHCat6=paste(Make,Cat6))
temp <- transform(temp, MakeWITHCat7=paste(Make,Cat7))
temp <- transform(temp, MakeWITHCat8=paste(Make,Cat8))
temp <- transform(temp, MakeWITHCat9=paste(Make,Cat9))
temp <- transform(temp, MakeWITHCat10=paste(Make,Cat10))
temp <- transform(temp, MakeWITHCat11=paste(Make,Cat11))
temp <- transform(temp, MakeWITHCat12=paste(Make,Cat12))
temp <- transform(temp, MakeWITHOrdCat=paste(Make,OrdCat))
temp <- transform(temp, MakeWITHNVCat=paste(Make,NVCat))
temp <- transform(temp, Cat1WITHCat2=paste(Cat1,Cat2))
temp <- transform(temp, Cat1WITHCat3=paste(Cat1,Cat3))
temp <- transform(temp, Cat1WITHCat4=paste(Cat1,Cat4))
temp <- transform(temp, Cat1WITHCat5=paste(Cat1,Cat5))
temp <- transform(temp, Cat1WITHCat6=paste(Cat1,Cat6))
temp <- transform(temp, Cat1WITHCat7=paste(Cat1,Cat7))
temp <- transform(temp, Cat1WITHCat8=paste(Cat1,Cat8))
temp <- transform(temp, Cat1WITHCat9=paste(Cat1,Cat9))
temp <- transform(temp, Cat1WITHCat10=paste(Cat1,Cat10))
temp <- transform(temp, Cat1WITHCat11=paste(Cat1,Cat11))
temp <- transform(temp, Cat1WITHCat12=paste(Cat1,Cat12))
temp <- transform(temp, Cat1WITHOrdCat=paste(Cat1,OrdCat))
temp <- transform(temp, Cat1WITHNVCat=paste(Cat1,NVCat))
temp <- transform(temp, Cat2WITHCat3=paste(Cat2,Cat3))
temp <- transform(temp, Cat2WITHCat4=paste(Cat2,Cat4))
temp <- transform(temp, Cat2WITHCat5=paste(Cat2,Cat5))
temp <- transform(temp, Cat2WITHCat6=paste(Cat2,Cat6))
temp <- transform(temp, Cat2WITHCat7=paste(Cat2,Cat7))
temp <- transform(temp, Cat2WITHCat8=paste(Cat2,Cat8))
temp <- transform(temp, Cat2WITHCat9=paste(Cat2,Cat9))
temp <- transform(temp, Cat2WITHCat10=paste(Cat2,Cat10))
temp <- transform(temp, Cat2WITHCat11=paste(Cat2,Cat11))
temp <- transform(temp, Cat2WITHCat12=paste(Cat2,Cat12))
temp <- transform(temp, Cat2WITHOrdCat=paste(Cat2,OrdCat))
temp <- transform(temp, Cat2WITHNVCat=paste(Cat2,NVCat))
temp <- transform(temp, Cat3WITHCat4=paste(Cat3,Cat4))
temp <- transform(temp, Cat3WITHCat5=paste(Cat3,Cat5))
temp <- transform(temp, Cat3WITHCat6=paste(Cat3,Cat6))
temp <- transform(temp, Cat3WITHCat7=paste(Cat3,Cat7))
temp <- transform(temp, Cat3WITHCat8=paste(Cat3,Cat8))
temp <- transform(temp, Cat3WITHCat9=paste(Cat3,Cat9))
temp <- transform(temp, Cat3WITHCat10=paste(Cat3,Cat10))
temp <- transform(temp, Cat3WITHCat11=paste(Cat3,Cat11))
temp <- transform(temp, Cat3WITHCat12=paste(Cat3,Cat12))
temp <- transform(temp, Cat3WITHOrdCat=paste(Cat3,OrdCat))
temp <- transform(temp, Cat3WITHNVCat=paste(Cat3,NVCat))
temp <- transform(temp, Cat4WITHCat5=paste(Cat4,Cat5))
temp <- transform(temp, Cat4WITHCat6=paste(Cat4,Cat6))
temp <- transform(temp, Cat4WITHCat7=paste(Cat4,Cat7))
temp <- transform(temp, Cat4WITHCat8=paste(Cat4,Cat8))
temp <- transform(temp, Cat4WITHCat9=paste(Cat4,Cat9))
temp <- transform(temp, Cat4WITHCat10=paste(Cat4,Cat10))
temp <- transform(temp, Cat4WITHCat11=paste(Cat4,Cat11))
temp <- transform(temp, Cat4WITHCat12=paste(Cat4,Cat12))
temp <- transform(temp, Cat4WITHOrdCat=paste(Cat4,OrdCat))
temp <- transform(temp, Cat4WITHNVCat=paste(Cat4,NVCat))
temp <- transform(temp, Cat5WITHCat6=paste(Cat5,Cat6))
temp <- transform(temp, Cat5WITHCat7=paste(Cat5,Cat7))
temp <- transform(temp, Cat5WITHCat8=paste(Cat5,Cat8))
temp <- transform(temp, Cat5WITHCat9=paste(Cat5,Cat9))
temp <- transform(temp, Cat5WITHCat10=paste(Cat5,Cat10))
temp <- transform(temp, Cat5WITHCat11=paste(Cat5,Cat11))
temp <- transform(temp, Cat5WITHCat12=paste(Cat5,Cat12))
temp <- transform(temp, Cat5WITHOrdCat=paste(Cat5,OrdCat))
temp <- transform(temp, Cat5WITHNVCat=paste(Cat5,NVCat))
temp <- transform(temp, Cat6WITHCat7=paste(Cat6,Cat7))
temp <- transform(temp, Cat6WITHCat8=paste(Cat6,Cat8))
temp <- transform(temp, Cat6WITHCat9=paste(Cat6,Cat9))
temp <- transform(temp, Cat6WITHCat10=paste(Cat6,Cat10))
temp <- transform(temp, Cat6WITHCat11=paste(Cat6,Cat11))
temp <- transform(temp, Cat6WITHCat12=paste(Cat6,Cat12))
temp <- transform(temp, Cat6WITHOrdCat=paste(Cat6,OrdCat))
temp <- transform(temp, Cat6WITHNVCat=paste(Cat6,NVCat))
temp <- transform(temp, Cat7WITHCat8=paste(Cat7,Cat8))
temp <- transform(temp, Cat7WITHCat9=paste(Cat7,Cat9))
temp <- transform(temp, Cat7WITHCat10=paste(Cat7,Cat10))
temp <- transform(temp, Cat7WITHCat11=paste(Cat7,Cat11))
temp <- transform(temp, Cat7WITHCat12=paste(Cat7,Cat12))
temp <- transform(temp, Cat7WITHOrdCat=paste(Cat7,OrdCat))
temp <- transform(temp, Cat7WITHNVCat=paste(Cat7,NVCat))
temp <- transform(temp, Cat8WITHCat9=paste(Cat8,Cat9))
temp <- transform(temp, Cat8WITHCat10=paste(Cat8,Cat10))
temp <- transform(temp, Cat8WITHCat11=paste(Cat8,Cat11))
temp <- transform(temp, Cat8WITHCat12=paste(Cat8,Cat12))
temp <- transform(temp, Cat8WITHOrdCat=paste(Cat8,OrdCat))
temp <- transform(temp, Cat8WITHNVCat=paste(Cat8,NVCat))
temp <- transform(temp, Cat9WITHCat10=paste(Cat9,Cat10))
temp <- transform(temp, Cat9WITHCat11=paste(Cat9,Cat11))
temp <- transform(temp, Cat9WITHCat12=paste(Cat9,Cat12))
temp <- transform(temp, Cat9WITHOrdCat=paste(Cat9,OrdCat))
temp <- transform(temp, Cat9WITHNVCat=paste(Cat9,NVCat))
temp <- transform(temp, Cat10WITHCat11=paste(Cat10,Cat11))
temp <- transform(temp, Cat10WITHCat12=paste(Cat10,Cat12))
temp <- transform(temp, Cat10WITHOrdCat=paste(Cat10,OrdCat))
temp <- transform(temp, Cat10WITHNVCat=paste(Cat10,NVCat))
temp <- transform(temp, Cat11WITHCat12=paste(Cat11,Cat12))
temp <- transform(temp, Cat11WITHOrdCat=paste(Cat11,OrdCat))
temp <- transform(temp, Cat11WITHNVCat=paste(Cat11,NVCat))
temp <- transform(temp, Cat12WITHOrdCat=paste(Cat12,OrdCat))
temp <- transform(temp, Cat12WITHNVCat=paste(Cat12,NVCat))
temp <- transform(temp, OrdCatWITHNVCat=paste(OrdCat,NVCat))

training <- temp

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.

temp <- testing
temp <- as.data.frame(temp)
temp <- cbind(temp[1:14],temp[23],temp[15:22],temp[24:28])
names <- colnames(temp)

temp <- transform(temp, MakeWITHCat1=paste(Make,Cat1))
temp <- transform(temp, MakeWITHCat2=paste(Make,Cat2))
temp <- transform(temp, MakeWITHCat3=paste(Make,Cat3))
temp <- transform(temp, MakeWITHCat4=paste(Make,Cat4))
temp <- transform(temp, MakeWITHCat5=paste(Make,Cat5))
temp <- transform(temp, MakeWITHCat6=paste(Make,Cat6))
temp <- transform(temp, MakeWITHCat7=paste(Make,Cat7))
temp <- transform(temp, MakeWITHCat8=paste(Make,Cat8))
temp <- transform(temp, MakeWITHCat9=paste(Make,Cat9))
temp <- transform(temp, MakeWITHCat10=paste(Make,Cat10))
temp <- transform(temp, MakeWITHCat11=paste(Make,Cat11))
temp <- transform(temp, MakeWITHCat12=paste(Make,Cat12))
temp <- transform(temp, MakeWITHOrdCat=paste(Make,OrdCat))
temp <- transform(temp, MakeWITHNVCat=paste(Make,NVCat))
temp <- transform(temp, Cat1WITHCat2=paste(Cat1,Cat2))
temp <- transform(temp, Cat1WITHCat3=paste(Cat1,Cat3))
temp <- transform(temp, Cat1WITHCat4=paste(Cat1,Cat4))
temp <- transform(temp, Cat1WITHCat5=paste(Cat1,Cat5))
temp <- transform(temp, Cat1WITHCat6=paste(Cat1,Cat6))
temp <- transform(temp, Cat1WITHCat7=paste(Cat1,Cat7))
temp <- transform(temp, Cat1WITHCat8=paste(Cat1,Cat8))
temp <- transform(temp, Cat1WITHCat9=paste(Cat1,Cat9))
temp <- transform(temp, Cat1WITHCat10=paste(Cat1,Cat10))
temp <- transform(temp, Cat1WITHCat11=paste(Cat1,Cat11))
temp <- transform(temp, Cat1WITHCat12=paste(Cat1,Cat12))
temp <- transform(temp, Cat1WITHOrdCat=paste(Cat1,OrdCat))
temp <- transform(temp, Cat1WITHNVCat=paste(Cat1,NVCat))
temp <- transform(temp, Cat2WITHCat3=paste(Cat2,Cat3))
temp <- transform(temp, Cat2WITHCat4=paste(Cat2,Cat4))
temp <- transform(temp, Cat2WITHCat5=paste(Cat2,Cat5))
temp <- transform(temp, Cat2WITHCat6=paste(Cat2,Cat6))
temp <- transform(temp, Cat2WITHCat7=paste(Cat2,Cat7))
temp <- transform(temp, Cat2WITHCat8=paste(Cat2,Cat8))
temp <- transform(temp, Cat2WITHCat9=paste(Cat2,Cat9))
temp <- transform(temp, Cat2WITHCat10=paste(Cat2,Cat10))
temp <- transform(temp, Cat2WITHCat11=paste(Cat2,Cat11))
temp <- transform(temp, Cat2WITHCat12=paste(Cat2,Cat12))
temp <- transform(temp, Cat2WITHOrdCat=paste(Cat2,OrdCat))
temp <- transform(temp, Cat2WITHNVCat=paste(Cat2,NVCat))
temp <- transform(temp, Cat3WITHCat4=paste(Cat3,Cat4))
temp <- transform(temp, Cat3WITHCat5=paste(Cat3,Cat5))
temp <- transform(temp, Cat3WITHCat6=paste(Cat3,Cat6))
temp <- transform(temp, Cat3WITHCat7=paste(Cat3,Cat7))
temp <- transform(temp, Cat3WITHCat8=paste(Cat3,Cat8))
temp <- transform(temp, Cat3WITHCat9=paste(Cat3,Cat9))
temp <- transform(temp, Cat3WITHCat10=paste(Cat3,Cat10))
temp <- transform(temp, Cat3WITHCat11=paste(Cat3,Cat11))
temp <- transform(temp, Cat3WITHCat12=paste(Cat3,Cat12))
temp <- transform(temp, Cat3WITHOrdCat=paste(Cat3,OrdCat))
temp <- transform(temp, Cat3WITHNVCat=paste(Cat3,NVCat))
temp <- transform(temp, Cat4WITHCat5=paste(Cat4,Cat5))
temp <- transform(temp, Cat4WITHCat6=paste(Cat4,Cat6))
temp <- transform(temp, Cat4WITHCat7=paste(Cat4,Cat7))
temp <- transform(temp, Cat4WITHCat8=paste(Cat4,Cat8))
temp <- transform(temp, Cat4WITHCat9=paste(Cat4,Cat9))
temp <- transform(temp, Cat4WITHCat10=paste(Cat4,Cat10))
temp <- transform(temp, Cat4WITHCat11=paste(Cat4,Cat11))
temp <- transform(temp, Cat4WITHCat12=paste(Cat4,Cat12))
temp <- transform(temp, Cat4WITHOrdCat=paste(Cat4,OrdCat))
temp <- transform(temp, Cat4WITHNVCat=paste(Cat4,NVCat))
temp <- transform(temp, Cat5WITHCat6=paste(Cat5,Cat6))
temp <- transform(temp, Cat5WITHCat7=paste(Cat5,Cat7))
temp <- transform(temp, Cat5WITHCat8=paste(Cat5,Cat8))
temp <- transform(temp, Cat5WITHCat9=paste(Cat5,Cat9))
temp <- transform(temp, Cat5WITHCat10=paste(Cat5,Cat10))
temp <- transform(temp, Cat5WITHCat11=paste(Cat5,Cat11))
temp <- transform(temp, Cat5WITHCat12=paste(Cat5,Cat12))
temp <- transform(temp, Cat5WITHOrdCat=paste(Cat5,OrdCat))
temp <- transform(temp, Cat5WITHNVCat=paste(Cat5,NVCat))
temp <- transform(temp, Cat6WITHCat7=paste(Cat6,Cat7))
temp <- transform(temp, Cat6WITHCat8=paste(Cat6,Cat8))
temp <- transform(temp, Cat6WITHCat9=paste(Cat6,Cat9))
temp <- transform(temp, Cat6WITHCat10=paste(Cat6,Cat10))
temp <- transform(temp, Cat6WITHCat11=paste(Cat6,Cat11))
temp <- transform(temp, Cat6WITHCat12=paste(Cat6,Cat12))
temp <- transform(temp, Cat6WITHOrdCat=paste(Cat6,OrdCat))
temp <- transform(temp, Cat6WITHNVCat=paste(Cat6,NVCat))
temp <- transform(temp, Cat7WITHCat8=paste(Cat7,Cat8))
temp <- transform(temp, Cat7WITHCat9=paste(Cat7,Cat9))
temp <- transform(temp, Cat7WITHCat10=paste(Cat7,Cat10))
temp <- transform(temp, Cat7WITHCat11=paste(Cat7,Cat11))
temp <- transform(temp, Cat7WITHCat12=paste(Cat7,Cat12))
temp <- transform(temp, Cat7WITHOrdCat=paste(Cat7,OrdCat))
temp <- transform(temp, Cat7WITHNVCat=paste(Cat7,NVCat))
temp <- transform(temp, Cat8WITHCat9=paste(Cat8,Cat9))
temp <- transform(temp, Cat8WITHCat10=paste(Cat8,Cat10))
temp <- transform(temp, Cat8WITHCat11=paste(Cat8,Cat11))
temp <- transform(temp, Cat8WITHCat12=paste(Cat8,Cat12))
temp <- transform(temp, Cat8WITHOrdCat=paste(Cat8,OrdCat))
temp <- transform(temp, Cat8WITHNVCat=paste(Cat8,NVCat))
temp <- transform(temp, Cat9WITHCat10=paste(Cat9,Cat10))
temp <- transform(temp, Cat9WITHCat11=paste(Cat9,Cat11))
temp <- transform(temp, Cat9WITHCat12=paste(Cat9,Cat12))
temp <- transform(temp, Cat9WITHOrdCat=paste(Cat9,OrdCat))
temp <- transform(temp, Cat9WITHNVCat=paste(Cat9,NVCat))
temp <- transform(temp, Cat10WITHCat11=paste(Cat10,Cat11))
temp <- transform(temp, Cat10WITHCat12=paste(Cat10,Cat12))
temp <- transform(temp, Cat10WITHOrdCat=paste(Cat10,OrdCat))
temp <- transform(temp, Cat10WITHNVCat=paste(Cat10,NVCat))
temp <- transform(temp, Cat11WITHCat12=paste(Cat11,Cat12))
temp <- transform(temp, Cat11WITHOrdCat=paste(Cat11,OrdCat))
temp <- transform(temp, Cat11WITHNVCat=paste(Cat11,NVCat))
temp <- transform(temp, Cat12WITHOrdCat=paste(Cat12,OrdCat))
temp <- transform(temp, Cat12WITHNVCat=paste(Cat12,NVCat))
temp <- transform(temp, OrdCatWITHNVCat=paste(OrdCat,NVCat))

testing <- temp
#selector
chiTest <- chi.squared(Class~., training)
cfsTest <- cfs(Class~., training)
infoTest <- information.gain(Class~., training)


#36
formula = Class ~ Cat12WITHNVCat + Cat12WITHNVCat + Cat11WITHNVCat + Cat10WITHNVCat + Cat9WITHNVCat + Cat8WITHNVCat + Cat7WITHNVCat + Cat6WITHNVCat + Cat5WITHNVCat + Cat4WITHNVCat + Cat3WITHNVCat + Cat3WITHCat7 + Cat2WITHNVCat + Cat1WITHNVCat + Cat1WITHCat3 + MakeWITHNVCat + MakeWITHOrdCat + MakeWITHCat12 + MakeWITHCat11 + MakeWITHCat10 + MakeWITHCat9 + MakeWITHCat8 + MakeWITHCat7 + MakeWITHCat6 + MakeWITHCat5 + MakeWITHCat4 + MakeWITHCat3 + MakeWITHCat2 + MakeWITHCat1 + NVCat + Var2 + Var6 + NVVar2 + NVVar3 + YearDiff

model <- naiveBayes(formula, training, laplace = 1)

pred <- predict(model, newdata= testing,type='raw')
#36
formula = Class ~ Cat12WITHNVCat + Cat12WITHNVCat + Cat11WITHNVCat + Cat10WITHNVCat + Cat9WITHNVCat + Cat8WITHNVCat + Cat7WITHNVCat + Cat6WITHNVCat + Cat5WITHNVCat + Cat4WITHNVCat + Cat3WITHNVCat + Cat3WITHCat7 + Cat2WITHNVCat + Cat1WITHNVCat + Cat1WITHCat3 + MakeWITHNVCat + MakeWITHOrdCat + MakeWITHCat12 + MakeWITHCat11 + MakeWITHCat10 + MakeWITHCat9 + MakeWITHCat8 + MakeWITHCat7 + MakeWITHCat6 + MakeWITHCat5 + MakeWITHCat4 + MakeWITHCat3 + MakeWITHCat2 + MakeWITHCat1 + NVCat + Var2 + Var6 + NVVar2 + NVVar3 + YearDiff

model <- glm(Class ~ Cat9 + NVCat + Var2 + Var6 + NVVar2 + NVVar3 + YearDiff + MakeWITHNVCat + Cat3WITHCat7 + Cat4WITHNVCat, data = training, family = "binomial", method = "glm.fit")

val <- as.data.frame(training)
val1 <- val[1:32000,]
val2 <- val[32001:38720,]

val$Class <- as.numeric(val$Class)-1

model <- glm(Class ~  Var1 + Var2 + Var6 + NVVar2 + NVVar3 + YearDiff + MakeWITHNVCat + Cat3WITHCat7 + Cat4WITHNVCat, data = val1, laplace = 1)

model <- glm(Class ~  Var1+Var2+Var3+Var4+Var5+Var6+Var7+Var8+NVVar1+NVVar2+NVVar3+NVVar4+YearDiff, data = training, family = binomial)

model <- glm(Class ~ Cat9 + NVCat + Var2 + Var6 + NVVar2 + NVVar3 + YearDiff +  Cat3WITHCat7 + Cat4WITHNVCat + OrdCat, data = training, family = "binomial", method = "glm.fit")

pred <- predict(model, newdata= testing, type='response')
submission$id <- test$RowID
submission$Yes <- pred
submission$No <- 1 - pred
write.csv(submission, "submission36.csv",row.names=FALSE)


t <- table(pred, val$Class)
confusionMatrix(t)
train.probs = predict(model, newdata= traininig, type="response")
actual = subset(training, select = c(Class))
actual$No.Class = 1 - actual$Class
model.pred = data.frame(train.probs, 1 - train.probs)
LogLoss(actual, model.pred)
LogLoss <- function(act, pred)
{
    eps = 1e-15;
    nr <- nrow(pred)
    pred = matrix(sapply( pred, function(x) max(eps,x)), nrow = nr)
    pred = matrix(sapply( pred, function(x) min(1-eps,x)), nrow = nr)
    ll = sum(act*log(pred) )
    ll = ll * -1/(nrow(act))
    return(ll);
}