How to update a glm model that contains NA's after fitting? error Number of observation not equal

StackOverflow https://stackoverflow.com/questions/23469069

  •  15-07-2023
  •  | 
  •  

Question

I have a dataset that contains some missing values (on independent variables). I’m fitting a glm model :

f.model=glm(data = data, formula = y~x1 +x2, "binomial", na.action =na.omit )

After this model I want the ‘null’ model , so I used update:

n.model=update(f.model, . ~ 1)

This seems to work, but the number of observations in both models differ (f.model n=234; n.model n=235). So when I try to estimate a likelihood ratio I get an error: Number of observation not equal!!.

Q: How to update the model so that it accounts for the missing values?

Was it helpful?

Solution

Although it is a bit strange that na.action =na.omit dit not solve the NA problem. I decided to filter out the data.

library(epicalc) # for lrtest

vars=c(“y”, “x1”, “x2”) #variables in the model
n.data=data[,vars] #filter data

f.model=glm(data = data, formula = y~x1 +x2, binomial)

n.model=update(f.model, . ~ 1)

LR= lrtest(n.model,f.model)

If someone has a better solution or an argument way na.action in combination with update results in unequal observations, your answer or solution is more than welcome!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top