multinom()默认如何处理NA值?

时间:2016-01-11 21:30:24

标签: r na missing-data logistic-regression multinomial

当我正在运行multinom()时,请说Y ~ X1 + X2 + X3,如果某一特定行X1NA(即缺失),则为Y,{{ 1}}和X2都有一个值,这整行会被抛出(就像在SAS中一样)吗?如何在X3中处理缺失值?

2 个答案:

答案 0 :(得分:1)

以下是一个简单示例(来自?multinom包的nnet),以探索不同的na.action

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

有意创建NA值:

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

测试4种不同的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)

因此na.exclude会在预测中生成NA,而na.omit会完全忽略它。 na.passna.fail不会创建模型。 如果未指定na.action,则会显示默认值:

> getOption("na.action")
[1] "na.omit"

答案 1 :(得分:0)

您可以指定行为

- 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