首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >R-方差分析中的似然比检验误差

R-方差分析中的似然比检验误差
EN

Stack Overflow用户
提问于 2017-09-06 14:39:52
回答 1查看 2.7K关注 0票数 1

我试图使用lrtest()在R中运行一个似然比测试,但是它给我带来了一些我无法修复的错误:

代码语言:javascript
复制
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()。这两者都是可以互换的吗?还是我会忽略任何有用的分析,用方差分析代替lrtest?

编辑:这是来自file.csv的数据集的一个示例。

代码语言:javascript
复制
   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
EN

Stack Overflow用户

回答已采纳

发布于 2017-09-06 19:08:25

我不认为你的代码有问题。您所遇到的错误表明,dat1和dat2不是模型,而是数据框架。也许您没有正确地执行代码的第2和第3行?

建议您完全重新启动R并尝试如下:

代码语言:javascript
复制
require(lmtest)
dat=structure(list(n = 1:20, Density = c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L), Species = structure(c(2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L), .Label = c("N", "NN"), class = "factor"), Mass = c(1.29, 2.84, 2.58, 2.81, 2.69, 2.12, 2.3, 1.95, 2.35, 2.28, 0.9, 2.33, 0.81, 1.37, 1.01, 1.94, 2.49, 2.13, 1.9, 1.46), Contact = c(0L, 1L, 0L, 1L, 0L, 1L, 1L, 0L, 0L, 1L, 0L, 0L, 1L, 1L, 1L, 0L, 0L, 0L, 0L, 0L)), .Names = c("n", "Density", "Species", "Mass", "Contact"), class = "data.frame", row.names = c(NA, -20L))
dat1<-glm(Contact~Density + Species, data=dat, family=binomial)
dat2<-glm(Contact~Density + Species + Mass, data=dat, family = binomial)
lrtest(dat1, dat2)

如果它不起作用,唯一可能发生不同情况的地方是当您读取csv文件时。

这说明了为什么你应该让别人更容易地重现你的问题。特别是,您应该允许其他人轻松地加载一些示例数据。Ale甚至给了你一个链接来解释如何做这件事。

票数 3
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46078116

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档