0

I would like to create a loop with a neural net and logistic regression to compare the bootstrapped MSE between the two. My issue is that my formulation of the MSE statistic makes the boot function not work. I went ahead and created some simulated data to illustrate the problem. If you are familiar with Günther, F., & Fritsch, S. (2010), you should recognize the coding structure right away.

variable1 <- rnorm(n = 1000, mean = 26, sd = 5)
variable2 <- rnorm(n = 1000, mean = 600, sd = 100)
variable3 <- rnorm(n = 1000, mean = 115, sd = 15)
 group <- 1 
 weight <- ((.3*(variable1/36))+(.3*(variable2/800))+(.3*(variable3/145)))

pt1 <- cbind (group, variable1, variable2, variable3, weight)

variable1 <- rnorm(n = 1000, mean = 21, sd = 5)
variable2 <- rnorm(n = 1000, mean = 500, sd = 100)
variable3 <- rnorm(n = 1000, mean = 100, sd = 15)
group <- 0
weight <- ((.3*(variable1/36))+(.3*(variable2/800))+(.3*(variable3/145)))

pt2  <- cbind (group, variable1, variable2, variable3, weight)

pt3 <- as.data.frame(rbind (pt1, pt2))

Passing <- rbinom (n = 1000, size = 1, prob = pt3$weight)

Data <- as.data.frame (cbind(pt3, Passing))
View (Data)

data <- Data[, sapply(Data, is.numeric)]
maxValue <- as.numeric(apply (data, 2, max))
minValue <- as.numeric(apply (data, 2, min))

data_scaled <- as.data.frame(scale(data, center = minValue, 
scale = maxValue-minValue))

ind <- sample (1:nrow(data_scaled), 500)
train <- data_scaled[ind,]
test <- data_scaled[-ind,]


model <- glm (formula = 
  Passing ~ variable1 + variable2 + variable3, 
  family = "binomial", 
  data = train)

summary (model)
predicted_model <- predict(model,test)


nueral_model <- neuralnet(formula = 
  Passing ~ variable1 + variable2 + variable3,
  hidden = 3 ,threshold = 0.01, linear.output=FALSE, data= train)

plot (nueral_model)
results <- compute (nueral_model, test[2:4])


results <- results$net.result*(max(data$Passing)-
min(data$Passing))+ min(data$Passing)
Values <- (test$Passing)*(max(data$Passing)- 
min(data$Passing)) + min(data$Passing)

MSE_nueral_model <- sum((results - Values)^2)/nrow(test)
MSE_model <- sum((predicted_model - test$Passing)^2)/nrow(test)
print(paste(MSE_nueral_model,MSE_model))

Usually with a loop, I can use the for command but in this case, I am a little lost on looping a neural net.

Any help would be greatly appreciated.

StupidWolf
  • 45,075
  • 17
  • 40
  • 72
JWH2006
  • 239
  • 1
  • 11
  • You forgot to post the error (after "following error" in third last line). Also, where is `for` loop in code? – rnso Feb 07 '18 at 17:52
  • Thanks for the reply, I had edited the code earlier and no longer have the error. Now the issue is I am not 1000% sure how to formulate the code using the for command. In terms of computational efficiency and in terms of actual coding. – JWH2006 Feb 07 '18 at 17:57
  • Do you need to `plot` also as part of `for` loop? If not, it can be removed from this code. Also, can you give link to code in Günther, F., & Fritsch, S. (2010)? Can you use `boot` function of `boot` package here (instead of `for` loop): https://www.statmethods.net/advstats/bootstrapping.html ? – rnso Feb 07 '18 at 18:13
  • Günther, F., & Fritsch, S. (2010). neuralnet: Training of neural networks. The R journal, 2(1), 30-38. And that is a really good catch about the plotting command I had embedded, that would have been a real pain. – JWH2006 Feb 07 '18 at 18:18
  • Can you use `boot` function of `boot` package here (instead of `for` loop): https://www.statmethods.net/advstats/bootstrapping.html ? – rnso Feb 07 '18 at 18:18

0 Answers0