1

I am trying to run a likelihood ratio test in R using lrtest() but it has been giving me errors that I haven't been able to fix:

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

According to this link, either ANOVA or lrtest can be used for the likelihood ratio test. I tried the ANOVA method and the test produced results, unlike when I tried using lrtest(). Are both of these interchangeable, or would I miss out on any useful analysis by using ANOVA instead of lrtest?

Edit: Here is a sample of the data set from 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
user6480584
  • 55
  • 1
  • 7
  • 1
    Can you give us a reproducible example? thanks! https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Ale Sep 06 '17 at 14:44
  • @CPak I edited it to show the output of both – user6480584 Sep 06 '17 at 15:04
  • Could you try this `dat1 <- glm(Species ~ Sepal.Length + Sepal.Width, data=iris)` `dat2 <- glm(Species ~ Sepal.Length + Sepal.Width + Petal.Length, data=iris)` `lrtest(dat1, dat2)`? See if you get the same error? – CPak Sep 06 '17 at 15:10
  • This is what I get when I input dat1: Error in glm.fit(x = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, : NA/NaN/Inf in 'y' In addition: Warning messages: 1: In Ops.factor(y, mu) : ‘-’ not meaningful for factors 2: In Ops.factor(eta, offset) : ‘-’ not meaningful for factors 3: In Ops.factor(y, mu) : ‘-’ not meaningful for factors – user6480584 Sep 06 '17 at 15:20
  • 1
    @Ale I uploaded a sample data set to go along with the code. Is that better? – user6480584 Sep 06 '17 at 15:59
  • now it work, or? – and-bri Sep 06 '17 at 17:25
  • @and-bri Still getting the same thing. – user6480584 Sep 06 '17 at 18:46
  • on my computer it work without error...maybe there is a problem with your raw data. can you post the output of `str(dat)` – and-bri Sep 06 '17 at 18:54
  • I retried it and somehow both ANOVA and lrtest worked this time – user6480584 Sep 06 '17 at 19:00
  • nice. than please mark the question solved. – and-bri Sep 06 '17 at 19:13

1 Answers1

3

I don't think there's a problem with your code. The error you're getting suggests that dat1 and dat2 are not models but data frames. Perhaps you didn't execute lines 2 and 3 of your code correctly?

Suggesting you restart your R completely and try this:

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)

If it doesn't work, the only place where something could have happened differently is when you read in the csv file.

This shows why you should make it easier for others to reproduce your problem. In particular you should allow others to easily load some sample data. Ale even gave you a link explaining how to do it.

Lukasz
  • 468
  • 4
  • 8