简体   繁体   English

在 R 中,您可以手动设置 lm() 或 Anova() 的自由度吗?

[英]In R can you manually set degrees of freedom for lm() or Anova()?

I am replicating SPSS code in R that runs several Type 3 ANOVAs.我正在 R 中复制 SPSS 代码,该代码运行多个 3 类方差分析。 In SPSS you can specify specific contrasts in an ANOVA (eg, compare level 2 v level 4 in this 5-level variable).在 SPSS 中,您可以在 ANOVA 中指定特定的对比(例如,比较这个 5 级变量中的 2 级和 4 级)。 The resulting ANOVA tables return a test where the degrees of freedom are equal to the full sample, rather than the sample that is just concentrated in those two levels.生成的 ANOVA 表返回自由度等于完整样本的检验,而不是仅集中在这两个水平上的样本。

In R, I use the command below to run an ANOVA comparing those two levels but the resulting Residuals DF is based on the subsample of only those two levels rather than the full sample.在 R 中,我使用以下命令运行 ANOVA 比较这两个级别,但生成的残差 DF 仅基于这两个级别的子样本,而不是完整样本。 Is there a way I can manually set the DF in either the lm() or Anova() function to avoid this issue?有没有办法可以在 lm() 或 Anova() function 中手动设置 DF 以避免此问题? Or is there a way to specify contrasts that uses the full sample DF?或者有没有办法指定使用完整样本 DF 的对比?

Anova(lm(DV ~ FiveLevelFactor, data = data, type = 3, subset = FiveLevelFactor == "2" | FiveLevelFactor == "4"))

How about using the linearHypothesis() function from the car package:使用package car上的linearHypothesis() function怎么样:

library(car)
data(Ornstein)

mod <- lm(interlocks ~ log(assets) + sector + nation, data=Ornstein)
linearHypothesis(mod, "nationUK = nationUS")
# Linear hypothesis test
# 
# Hypothesis:
#   nationUK - nationUS = 0
# 
# Model 1: restricted model
# Model 2: interlocks ~ log(assets) + sector + nation
# 
#   Res.Df   RSS Df Sum of Sq      F Pr(>F)
# 1    235 29829                           
# 2    234 29690  1    138.36 1.0904 0.2975

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM