0
votes

May I have your minutes for my question below? I was doing linear regression and want to compare my models with a likelihood ratio test. However, 1. The likelihood ratio test lrtest()seems not working for me. I mean, I did typing ?lrtest and couldn't get any documentation in R.

  1. Then, I moved to ANOVA(model1, model2, test ="LRT"). However, It gave me an error. "Error in UseMethod("anova"):no applicable method for 'anova' applied to an object of class "list" I used a data set of the iris as follows.

    data <- data.frame(B = c("m","m","m","m","m", "m", "f","f","f","f","f"),
               G = c("s","s","s","u","u", "u", "k","k","k","r","r"),
               ZN =c(78,82,34,67,98,56,37,45,27,18,34),
               GFR=c(120,100,90,60,100,110,100,90,95,87,96),
               g1 = c(35, 2, 3, 4, 5, 6, 7, 10, 12, 41, 76), 
               g2 = c(20, 2, 7, 2, 8, 5, 5, 3, 7, 2, 12), 
               g3 = c(5, 0, 4, 5, 2, 4, 8, 9, 20, 1, 11),
               g4 = c(1,3,4,5,7,3,1,5,7,3,10),
               g5 = c(20,23, 27, 35, 12, 10, 17, 24, 21, 15, 16),
               g6 = c(13,13,115,17,14,12,19,6,7,8,4),
               g7 = c(5, 0, 4, 5, 2, 4, 8, 9, 20, 1, 11),
               g8 = c(1,3,4,5,7,3,1,5,7,3,10),
               g9 = c(20,23, 27, 35, 12, 10, 17, 24, 21, 15, 16),
               g10 =c(13,13,115,17,14,12,19,6,7,8,4)); 
    
    zn12 <- lapply(data[,-c(1,2,4)], function(x) lm(GFR ~ x + ZN, data = data))
    zn13 <- lm(GFR ~ ZN, data = data)
    anova(zn12,zn13, test="LRT")
    

any suggestions would be helpful Thank you!

1
try ir1 <- lm(Sepal.Length ~ Petal.Length + Petal.Width, data = iris); ir2 <- lm(Sepal.Length ~ Petal.Length, data = iris); anova(ir1,ir2, test="LRT")Roman
Thanks Roman! However, if I have to design many predictor variables and one covariate with a response variable I must use the lappy function or one of the loop functions. let me edit my previous post and you may see my data setwork_withR
it's a list of lm ; you needa do anova(zn12[[1]],zn13...) ; so you iterate through your list, lapply(zn12,function(i)anova(i,zn13))StupidWolf

1 Answers

0
votes

You can use the approach presented in the comments or use a tidyverse

library(tidyverse)
data %>% 
  gather(EP, value, -B:-GFR) %>% 
  split(.$EP)  %>% 
  map(~anova(lm(GFR ~ ZN, data =.),
             lm(GFR ~ value + ZN, data =.),
             test="LRT"))

When you are for the p.values try

data %>% 
  gather(Fac, value, -B:-GFR) %>% 
  split(.$Fac)  %>% 
  map(~anova(lm(GFR ~ ZN, data =.),
             lm(GFR ~ value + ZN, data =.),
             test="LRT") %>%  
        broom::tidy(.) %>% 
        select(p.value) %>% 
        slice(2)) %>% 
  bind_rows(.id = "Factor")
# A tibble: 10 x 2
   Factor p.value
   <chr>    <dbl>
 1 g1     0.467  
 2 g10    0.797  
 3 g2     0.0323 
 4 g3     0.783  
 5 g4     0.403  
 6 g5     0.00556
 7 g6     0.797  
 8 g7     0.783  
 9 g8     0.403  
10 g9     0.00556