1
$\begingroup$

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?

$\endgroup$
  • $\begingroup$ Where is the Carseats dataset? $\endgroup$ – cdeterman Feb 4 '16 at 18:01
  • $\begingroup$ @cdeterman The Carseats dataset can be found in the ISLR package. $\endgroup$ – Von Kar Feb 4 '16 at 18:03
  • 1
    $\begingroup$ Did the answer below address your problem? If so, please accept it so the question can be marked as answered. If not, please elaborate on your question. $\endgroup$ – cdeterman Feb 15 '16 at 19:35
2
$\begingroup$

You can use the same method described in this question where you can use model.matrix to generate your 'dummy' variables.

library(ISLR)
data(Carseats)

m <- model.matrix(~0+Sales+CompPrice+Income+Advertising + Population + 
                      Price + ShelveLoc + Age + Education + Urban + US, 
                  data = Carseats)

You can then use the resulting m object with the new variables (quantitative variables won't be altered). Be sure to note the new column names.

colnames(m)
[1] "Sales"           "CompPrice"       "Income"          "Advertising"    
 [5] "Population"      "Price"           "ShelveLocBad"    "ShelveLocGood"  
 [9] "ShelveLocMedium" "Age"             "Education"       "UrbanYes"       
[13] "USYes"  
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.