使用神经网络和逻辑回归创建一个循环来比较R

时间:2018-02-07 17:39:39

标签: r neural-network bootstrapping

我想创建一个带有神经网络和逻辑回归的循环来比较两者之间的自举MSE。我的问题是我的MSE统计数据的制定使得启动功能不起作用。我继续创建了一些模拟数据来说明问题。如果您熟悉Günther,F。,& Fritsch,S。(2010),你应该立即认识到编码结构。

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))

通常使用循环,我可以使用for命令,但在这种情况下,我在循环神经网络时有点迷失。

非常感谢任何帮助。

0 个答案:

没有答案