I'm using the neuralnet
package in R to attempt to predict the median value of Sales
using all the other variables of the data set (Carseats
) available.
After verifying that no datapoint is missing and randomly split the data to a train and test set I ran across an error when normalizing the data.
maxs <- apply(data, 2, max)
mins <- apply(data, 2, min)
scaled <- as.data.frame(scale(data, center = mins, scale = maxs - mins))
Error in scale.default(data, center = mins, scale = maxs - mins) :
length of 'center' must equal the number of columns of 'x'
Since I'm new to this subject I did some research/experimenting before posting here. The problem seems to come from the qualitative variables of the data set Carseats
. Looking at the variables of the data set more closely yields the following list:
str(Carseats)
'data.frame': 400 obs. of 11 variables:
Sales : num 9.5 11.22 10.06 7.4 4.15 ...
CompPrice : num 138 111 113 117 141 124 115 136 132 132 ...
Income : num 73 48 35 100 64 113 105 81 110 113 ...
Advertising: num 11 16 10 4 3 13 0 15 0 0 ...
Population : num 276 260 269 466 340 501 45 425 108 131 ...
Price : num 120 83 80 97 128 72 108 120 124 124 ...
ShelveLoc : Factor w/ 3 levels "Bad","Good","Medium": 1 2 3 ...
Age : num 42 65 59 55 38 78 71 67 76 76 ...
Education : Ord.factor w/ 9 levels "10"<"11"<"12"<..: 8 1 3 ...
Urban : Factor w/ 2 levels "No","Yes": 2 2 2 2 2 ...
US : Factor w/ 2 levels "No","Yes": 2 2 2 2 1 ...
After removing the qualitative variables (i.e. ShelveLoc
, Education
, Urban
and US
) from the data set using the Carseats$variable <- NULL
command and repeating the same steps I was able to plot the neural network.
What is the explanation for the error message? How can I include the qualitative variables to build the neural network? Is there a way to transform qualitative variables to continuous variables and does this approach makes sense for the model?
Carseats
dataset? $\endgroup$ – cdeterman Feb 4 '16 at 18:01Carseats
dataset can be found in theISLR
package. $\endgroup$ – Von Kar Feb 4 '16 at 18:03