0

for some reason, my model is not running. I created a model matrix to run a simple model with the package neuralnet. I know it might be challenging to debug other people code especially without the data but in case you think you could assist me here is the code:

library(tidyverse)
library(neuralnet)

#Activity 1 Load Data


featchannels <-read.csv("features_channel.csv")
trainTargets <-read.table("traintargets.txt")


#Activity 2 Normalize every column of the features dataset using min-max 
normalization to range [0-1].

normalized <- function(x) {
return((x-min(x)) /(max(x) -min(x)))
}

featchannels <- normalized(featchannels)


#Activity 3 Add a target feature named response to the features dataset 
with 0-1 values read from trainTargets.txt, with 1 indicating P300 
response and 0 otherwise. 

colnames(trainTargets)[1] <- "State"
featchannels <- cbind(featchannels, trainTargets)
# Changing rows to P300 and others. 
featchannels <- within(featchannels, State <- factor(State, labels = 
c("Other", "P300")))
featchannels$State <- as.factor(featchannels$State)
#4. Take the first 3840 rows of the dataset as the training data set, and 
the remaining 960 rows as the testing data set. 

training <- featchannels[1:3840,]
testing <- featchannels[3841:4800,]
enter code here
#Activitry 6
#Creating model matrix before runing the model
df_comb_training <- training
y <- model.matrix(~ df_comb_training$State + 0, data = df_comb_training[, 
c('State'), drop=FALSE])
# fix up names for as.formula
y_feats <- gsub("^[^ ]+\\$", "", colnames(y))
colnames(y) <- y_feats

df_comb_training <- df_comb_training[, !(colnames(df_comb_training) == 
"State")]
feats <- colnames(df_comb_training)
df_comb_training <- cbind(y, df_comb_training)

# Concatenate strings
f <- paste(feats, collapse=' + ')
y_f <- paste(y_feats, collapse=' + ')
f <- paste(y_f, '~', f)

# Convert to formula
f <- as.formula(f)
model_h5 <- neuralnet(f, df_comb_training, stepmax = 1e+08, hidden = 5)
Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294
Joe Black
  • 1
  • 3
  • 4
    It is quite difficult to debug code without data. Please share a reproducible sample of your data - either simulated or actual. `dput()` works well for sharing a few rows of actual data, or use `set.seed()` and share code to simulate. You can check out the FAQ on [How to make a reproducible example in R](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) for lots more help and suggestions on that issue. – Gregor Thomas Apr 19 '19 at 20:22
  • 5
    Also, please be much more specific than "doesn't run". Does it crash R? Is there a warning? An error? If so, what is the message, and which line produces it? Or do you just get unexpected results? Something else? If there are multiple errors, focus on the firs one. You might also want to read the site help center on ["How to ask"](https://stackoverflow.com/help/mcve). – Gregor Thomas Apr 19 '19 at 20:25

0 Answers0