1

I am new to R, and I am running the multinomial logistic regression in R, but it keeps showing the error:

library(nnet)
train <-read.csv("train.csv", header = TRUE)
train <-multinom(Crime_code~Year+Month+Day+Weekday+Time+Hour+Area+District+Longitude+Latitude,data=train,size=11,maxit=1700,weights=1700)
## Error in model.frame.default(formula = Crime_code ~ Year + Month + Day +  : variable lengths differ (found for '(weights)')
summary(train)
## Error in object[[i]] : object of type 'closure' is not subsettable

Here is the data structure:

str(train)
'data.frame':   1760097 obs. of  12 variables:
$ DR        : int  120215758 160512269 112104909 121523303 162107991 
161407795 180409993 130911557 120405961 160221815 ...
$ Year      : int  2012 2015 2011 2012 2016 2016 2018 2013 2011 2016 ...
$ Month     : int  8 9 1 12 2 2 5 6 9 12 ...
$ Day       : int  5 1 18 9 24 24 18 3 1 12 ...
$ Weekday   : int  6 1 1 6 2 2 4 0 3 0 ...
$ Time      : int  2345 800 5 1200 1630 910 1910 1240 2000 1800 ...
$ Hour      : int  23 8 0 12 16 9 19 12 20 18 ...
$ Area      : int  2 5 21 15 21 14 4 9 4 2 ...
$ District  : int  201 513 2105 1583 2185 1453 489 964 416 233 ...
$ Longitude : num  -118 -118 -119 -118 -119 ...
$ Latitude  : num  34.1 33.8 34.2 34.1 34.2 ...
$ Crime_code: int  624 354 220 745 310 624 626 624 230 420 ...
Jennyx
  • 11
  • 1
  • 4
  • check to see if you have missing values/NA in variables used for regression. use: `apply(train, 2, function(x) sum(is.na(x)))` remove missing value cases and try again. – Mankind_008 Dec 02 '18 at 02:24

1 Answers1

0

The problem seems to be with your weights value. There is not very much documentation about this parameter that I can find, but if it is weights for a neural network, I would expect it to be either a matrix or a vector, one weight for every node. Have you tried just taking the ,weights= 1700 out?

Kevin Mc
  • 477
  • 4
  • 14
  • I tried to take the weights out, but there is still an error: 'Error in nnet.default(X, Y, w, mask = mask, size = 0, skip = TRUE, softmax = TRUE, : formal argument "size" matched by multiple actual arguments' – Jennyx Dec 02 '18 at 03:19
  • Then I would suggest removing the size variable as well. – Kevin Mc Dec 02 '18 at 03:38
  • I removed the size, but there is another error coming out: Error in nnet.default(X, Y, w, mask = mask, size = 0, skip = TRUE, softmax = TRUE, : too many (1656) weights – Jennyx Dec 02 '18 at 04:12
  • without a reproducible code ( https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example ), I don't think I can help. – Kevin Mc Dec 02 '18 at 04:23