【发布时间】:2020-10-16 11:09:42
【问题描述】:
我有这样的数据:
data_in <-read_table2("Condition grade block Q2_1 Q2_2 Q2_4 Q2_8 Q2_10 Q2_11 Q2_14
Treatment 8 LB-MS-3 3 3 3 2 1 2 2
Treatment 9 LB-HS-2 4 4 4 3 3 4 3
Treatment 7 LB-MS-1 3 4 3 3 2 2 4
Treatment 10 LB-HS-2 2 4 2 3 3 3 3
Control 9 LB-HS-1 2 4 4 2 3 2 2
Control 8 LB-MS-3 3 3 3 2 3 3 2
Control 10 LB-HS-2 4 4 3 3 3 2 2
Control 8 LB-MS-1 3 3 3 3 2 3 3
Control NA LB-MS-3 3 3 3 2 3 3 4
Control 8 LB-MS-1 4 4 4 4 3 3 3
Control 9 LB-HS-1 3 3 4 3 3 4 4
Treatment NA LB-MS-1 2 2 2 2 2 2 2
Treatment 8 LB-MS-1 3 3 3 2 2 2 2
Control 11 LB-HS-1 3 4 3 1 2 3 3
Treatment 10 LB-HS-1 3 4 2 2 3 4 3
Treatment 8 LB-MS-3 4 4 4 3 3 4 3
Control 7 LB-MS-3 4 2 3 2 3 2 2
Treatment 7 LB-MS-2 4 3 3 4 3 4 3
Control 7 LB-MS-3 3 3 3 2 2 2 3
Treatment 8 LB-MS-2 4 2 3 3 2 2 2
")
我想将“条件”作为二进制因变量运行,并将几个数字和字符变量作为自变量运行。我的期望是 R 会为我将字符变量变成虚拟变量。
model <- lm(condition~.,data=data_in)
summary(model)
但是,当我运行它时,我得到了 NA。我对统计数据不是很熟悉,因此感谢任何帮助解释。
【问题讨论】:
-
这似乎不是适合 Stack Overflow 的特定编程问题。如果您对适当使用各种统计方法有一般性问题,那么您应该通过Cross Validated 询问此类问题。你更有可能在那里得到更好的答案。
-
你需要
glmdata_in$Condition <- factor(data_in$Condition)# > glm(Condition ~ ., data = na.omit(data_in), family = "binomial") -
@akrun。不需要
na.omit作为glm的默认值,并且可能所有 R 建模函数都省略了 NA。 -
@Edward 我正在使用 lm 代码进行测试,但忘记删除 na.omit
-
如果您真的想在二进制因变量上运行 OLS,则使用
data_in$condition <- ifelse(data_in$condition=="Treatment", 1, 0)将字符变量转换为 0/1 变量。然后,您的lm命令应该可以正常运行。