How does multinom() treat NA values by default?

2019-03-04 10:52发布

When I am running multinom(), say Y ~ X1 + X2 + X3, if for one particular row X1 is NA (i.e. missing), but Y, X2 and X3 all have a value, would this entire row be thrown out (like it does in SAS)? How are missing values treated in multinom()?

2条回答
老娘就宠你
2楼-- · 2019-03-04 11:21

Here is a simple example (from ?multinom from the nnet package) to explore the different na.action:

> library(nnet)
> library(MASS)
> example(birthwt)
> (bwt.mu <- multinom(low ~ ., bwt))

Intentionally create a NA value:

> bwt[1,"age"]<-NA # Intentionally create NA value
> nrow(bwt)
[1] 189

Test the 4 different na.action:

> predict(multinom(low ~ ., bwt, na.action=na.exclude)) # Note length is 189
# weights:  12 (11 variable)
initial  value 130.311670
iter  10 value 97.622035
final  value 97.359978
converged
  [1] <NA> 0    0    0    0    0    0    0    0    0    0    0    1    1    0
 [16] 0    0    0    0    0    0    0    0    0    0    0    0    0    0    0
 ....

> predict(multinom(low ~ ., bwt, na.action=na.omit)) # Note length is 188
# weights:  12 (11 variable)
initial  value 130.311670
iter  10 value 97.622035
final  value 97.359978
converged
  [1] 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0
 [38] 0 0 0 0 0 0 1 1 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 1 1 0 0 0 0
 .....

> predict(multinom(low ~ ., bwt, na.action=na.fail))    # Generates error
Error in na.fail.default(list(low = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,  :
  missing values in object

> predict(multinom(low ~ ., bwt, na.action=na.pass))    # Generates error
Error in qr.default(X) : NA/NaN/Inf in foreign function call (arg 1)

So na.exclude generates a NA in the prediction while na.omit omits it entirely. na.pass and na.fail will not create the model. If na.action is not specified, this shows the default:

> getOption("na.action")
[1] "na.omit"
查看更多
\"骚年 ilove
3楼-- · 2019-03-04 11:25

You can specify the behaviour

- na.omit and na.exclude: returns the object with observations removed if they contain any missing values; differences between omitting and excluding NAs can be seen in some prediction and residual functions
- na.pass: returns the object unchanged
- na.fail: returns the object only if it contains no missing values

http://www.ats.ucla.edu/stat/r/faq/missing.htm

查看更多
登录 后发表回答