【问题标题】:Running a linear model in R with spreadsheet data在 R 中使用电子表格数据运行线性模型
【发布时间】:2019-03-08 08:53:57
【问题描述】:

我有一个数据集,包含 106 个人的两种类型 - a 和 b 具有各种变量,例如年龄和性别。我想运行一个线性模型,该模型根据协变量预测每个人是 a 型还是 b 型。

我读取了每个人的年龄、性别和类型标签的值:

`data = read.xlsx("spreadsheet.xlsx",2, as.is = TRUE)`
age = data$age
gender = data$gender
type = data$type

其中每个的形式为:

age = [28, 30, 19, 23 etc]
gender = [male, male, female, male etc]
type = [a b b b]

然后我尝试使用以下方法设置模型:

model1 = lm(type ~ age + gender)

但我收到此错误消息:

Warning messages:
1: In model.response(mf, "numeric") :
using type="numeric" with a factor response will be ignored
2: In Ops.factor(y, z$residuals) : - not meaningful for factors

我尝试使用以下方法更改类型、年龄和性别的格式:

age = as.numeric(as.character(age))
gender = as.character(gender)
type = as.character(type)

但这不起作用!

【问题讨论】:

  • 由于您的回答是二项式的,我认为您可以使用逻辑回归。可以使用您最喜欢的搜索引擎找到如何在 R 中执行此操作的教程。

标签: r


【解决方案1】:

您不能使用带有因子作为响应变量的线性回归模型,这是您在此处尝试执行的操作(类型是您的响应变量)。回归模型需要数值响应变量。相反,您应该查看分类模型。

正如 Roland 所指出的,您可能希望首先将您的“类型”变量重新表述为一个逻辑的二项式变量。您可以创建一个名为“is.type.a”的新变量,而不是具有两个级别“a”和“b”的称为“type”的因子,该变量将包含 TRUE 或 FALSE。

然后您可以尝试基于二项分布的逻辑回归

model <- glm(is.type.a ~ age + gender,data=data,family="binomial")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-06
    • 2021-12-30
    • 2013-12-11
    • 1970-01-01
    • 2013-09-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多