我正在尝试使用 lrtest() 在 R 中运行似然比测试,但它给了我无法修复的错误:
dat<-read.csv("file.csv", header=TRUE)
dat1<-glm(Contact~Density + Species, data=dat, family=binomial)
dat2<-glm(Contact~Density + Species + Mass, data=dat, family = binomial)
lrtest(dat1, dat2)
Error in UseMethod("logLik") :
no applicable method for 'logLik' applied to an object of class "data.frame"
> dat1
Call: glm(formula = Contact ~ Density + Species, family = binomial,
data = dat)
Coefficients:
(Intercept) Density SpeciesNN
-2.0615 0.2522 1.3870
Degrees of Freedom: 39 Total (i.e. Null); 37 Residual
Null Deviance: 54.55
Residual Deviance: 41.23 AIC: 47.23
> dat2
Call: glm(formula = Contact ~ Density + Species + Mass, family = binomial,
data = dat)
Coefficients:
(Intercept) Density SpeciesNN Mass
-2.5584 0.2524 1.4258 0.2357
Degrees of Freedom: 39 Total (i.e. Null); 36 Residual
Null Deviance: 54.55
Residual Deviance: 41.11 AIC: 49.11
根据此链接,ANOVA 或 lrtest 均可用于似然比检验。我尝试了 ANOVA 方法并且测试产生了结果,这与我尝试使用 lrtest() 时不同。这两者是否可以互换,或者我会通过使用 ANOVA 而不是 lrtest 错过任何有用的分析?
编辑:这是来自 file.csv 的数据集示例。
Density Species Mass Contact
1 2 NN 1.29 0
2 2 NN 2.84 1
3 2 NN 2.58 0
4 2 NN 2.81 1
5 2 NN 2.69 0
6 2 N 2.12 1
7 2 N 2.30 1
8 2 N 1.95 0
9 2 N 2.35 0
10 2 N 2.28 1
11 4 NN 0.90 0
12 4 NN 2.33 0
13 4 NN 0.81 1
14 4 NN 1.37 1
15 4 NN 1.01 1
16 4 N 1.94 0
17 4 N 2.49 0
18 4 N 2.13 0
19 4 N 1.90 0
20 4 N 1.46 0