我试图估计决定纽约和芝加哥人之间幸福水平差异的因素。
数据如下所示。
Happiness City Gender Employment Worktype Holiday
1 60 New York 0 0 Unemployed Unemployed
2 80 Chicago 1 1 Whitecolor 1 day a week
3 39 Chicago 0 0 Unemployed Unemployed
4 40 New York 1 0 Unemployed Unemployed
5 69 Chicago 1 1 Bluecolor 2 day a week
6 90 Chicago 1 1 Bluecolor 2 day a week
7 100 New York 0 1 Whitecolor 2 day a week
8 30 New York 1 1 Whitecolor 1 day a week
幸福水平是因变量,'城市'是人生活的地方。 '性别'编码为0 =男人1 =女人。 '工作'是0 =失业,1 =就业。 ' Worktype'是三个层面的因素:'失业',' Whitecolor' Bluecolor'。 '假日'是一个人在一周内休息多少天。在这里'城市','性别','工作类型'和'假日'变量都是因素。 '幸福'和就业'变量类型是数字。
我想估计的模型是
lm(Happiness ~ City + Gender + Employment:(Worktype + Holiday))
我离开了就业'价值作为数值,如果'就业'等于0(未使用),0:(工作类型+假日)= 0,因此模型自动缩减为
lm(Happiness ~ City + Gender)
对于失业人员。
但是,回归结果会返回NA值。
Coefficients: (2 not defined because of singularities)
Estimate Std. Error t value Pr(>|t|)
(Intercept) 56.75 23.56 2.408 0.138
CityNew York -14.50 27.21 -0.533 0.647
Gender1 -2.25 35.99 -0.063 0.956
Employment:WorktypeBluecolor 25.00 43.02 0.581 0.620
Employment:WorktypeUnemployed NA NA NA NA
Employment:WorktypeWhitecolor 57.75 35.99 1.604 0.250
Employment:Holiday1 day a week -50.00 54.42 -0.919 0.455
Employment:Holiday2 day a week NA NA NA NA
这似乎是由于“失业”和“失业”造成的。 '工作类型'中的价值和'假日'变量。但是,我不确定为什么R没有处理就业:WorktypeUnemployed显然为0:Worktype = 0为零并且不从模型中删除它。这是因为R将就业:HolidayUnemployed设为基线,两者都是完全多线性的吗? (我不得不把“失业”和“工作类型”和“假日”的价值放在一起,因为我希望看到'工作类型和'和'& #39;假日'与失业人员相比。如果我删除了“失业”值,NA会消失,但基线将是“白色&#39”和“#39” ;每周1天'所以我看不到与“失业”相比的效果。)
如果是这样,为什么我得到NA的系数为'员工:假期每周2天'?它似乎与“失业”无关。值。
只需删除NA系数,我可以依赖此结果吗?
下面是可重现的代码。
Happiness <- c(60, 80, 39, 40, 69, 90, 100, 30)
City <- as.factor(c("New York", "Chicago", "Chicago", "New York", "Chicago",
"Chicago", "New York", "New York"))
Gender <- as.factor(c(0, 1, 0, 1, 1, 1, 0, 1)) # 0 = man, 1 = woman.
Employment <- c(0,1, 0, 0, 1 ,1 , 1 , 1) # 0 = unemployed, 1 = employed.
Worktype <- as.factor(c("Unemployed", "Whitecolor", "Unemployed",
"Unemployed", "Bluecolor", "Bluecolor", "Whitecolor","Whitecolor"))
Holiday <- as.factor(c(0, 1, 0, 0, 2, 2, 2, 1))
levels(Holiday) <- c("Unemployed", "1 day a week", "2 day a week")
data <- data.frame(Happiness, City, Gender, Employment, Worktype, Holiday)
head(data,8)
str(data)
reg <- lm(Happiness ~ City + Gender + Employment:(Worktype + Holiday))
summary(reg)
答案 0 :(得分:2)
您不应该担心<div [product-data]="data" *ngFor="let data of products"></div>
的NA值。 R会自动尝试计算所有交互,但该特定系数仍未确定,因为很明显,就业= 1和工作类型=&#34;失业&#34;。它对其他系数的计算没有任何影响:您可以通过手动编码虚拟变量进行验证:
Employment:WorktypeUnemployed
即使> library(lme4) # for the convenient "dummy" function
> data <- data.frame(data,
+ dummy(Worktype, c("Bluecolor","Whitecolor")),
+ h1=dummy(Holiday)[,1],
+ h2=dummy(Holiday)[,2])
>
> reg <- lm(Happiness ~ City + Gender + Employment:Bluecolor + Employment:Whitecolor + Employment:h1 + Employment:h2 , data)
> summary(reg)
Call:
lm(formula = Happiness ~ City + Gender + Employment:Bluecolor +
Employment:Whitecolor + Employment:h1 + Employment:h2, data = data)
Residuals:
1 2 3 4 5 6 7 8
1.775e+01 1.775e+01 -1.775e+01 8.882e-16 -1.050e+01 1.050e+01 4.441e-15 -1.775e+01
Coefficients: (1 not defined because of singularities)
Estimate Std. Error t value Pr(>|t|)
(Intercept) 56.75 23.56 2.408 0.138
CityNew York -14.50 27.21 -0.533 0.647
Gender1 -2.25 35.99 -0.063 0.956
Employment:Bluecolor 25.00 43.02 0.581 0.620
Employment:Whitecolor 57.75 35.99 1.604 0.250
Employment:h1 -50.00 54.42 -0.919 0.455
Employment:h2 NA NA NA NA
Residual standard error: 27.21 on 2 degrees of freedom
Multiple R-squared: 0.6798, Adjusted R-squared: -0.1208
F-statistic: 0.8491 on 5 and 2 DF, p-value: 0.619
不再存在,估计的系数也是相同的。
但是,Employment:WorktypeUnemployed
(相当于Employment:h2
)的NA值仍然存在。这似乎是因为在这个简化的数据集中你最终得到了一个奇异的模型矩阵(即一列是其他列的线性组合)
Employment:Holiday2 day a week
因此,对于较大的数据集,此问题可能不存在。最终,您可以尝试删除模型中的任何冗余(例如,是否有任何人每周假期0天?如果没有,那么1天应该是基线,并且您将在假日的代码中添加额外的列代码&gt ; 1)。您可以使用> solve(crossprod(model.matrix(reg)))
Error in solve.default(crossprod(model.matrix(reg))) :
system is computationally singular: reciprocal condition number = 1.79897e-18
函数来检查哪个术语可以解决问题。