0

I’m trying to stratify my glm model by age decades.

My main question is: Instead of doing this subsetting within glm could I just make a new column called BYDECADE and run the the code as follows?

 fit <- glm(COMPLICATION~BYDECADE, 
                 data = data.clean, 
                 family = binomial)

AGE BYDECADE
43     40
45     40
24     20
21     20
23     20
37     30
34     30
36     30

The following works but obviously I don’t capture the decade just everyone above 50

fit <- glm(COMPLICATION~AGE, 
             data = subset(data.clean, AGE > 50), 
             family = binomial)

but the following does not work for me and I’m not sure how to go about it.

fit <- glm(COMPLICATION~AGE, 
             data = subset(data.clean, AGE > 50 & AGE <= 60), 
             family = binomial)
Dave2e
  • 22,192
  • 18
  • 42
  • 50
ComputerNoob
  • 480
  • 6
  • 12
  • 2
    Not quite sure what you are really asking. The answer to the first question is Yes, you could very well make that column and use it as you see fit. The rest of your code samples I don't see what they have to do with your first question? You are filtering on `AGE` larger than 50 in one case, and in another you are filtering out an AGE range of (50,60] . What does that have to do with rounding age to the nearest 10'er? – Sirius Mar 14 '21 at 14:29
  • Thank you that answers the questions pretty much in that I can just make a new column. What I was showing was that if filter AGE > 50 the code runs but if I add that extra bit to filter with AGE > 50 & AGE < 60 to get that decade stratification the code crashes. – ComputerNoob Mar 14 '21 at 14:38
  • 1
    Could you show the error? – Sirius Mar 14 '21 at 16:03
  • Error in `contrasts<-`(`*tmp*`, value = contr.funs[1 + isOF[nn]]) : contrasts can be applied only to factors with 2 or more levels – ComputerNoob Mar 14 '21 at 17:58
  • To that end I checked the variable AGE and it is a factor with > 2 levels. – ComputerNoob Mar 14 '21 at 18:10
  • Is `AGE` the deacde number at this point? I was under the impression you meant to call that variable BYDECADE and keep AGE as it were. Usually an age wouldn't be a factor with levels, it'd be a numeric. Can you show the relevant data for your error. None of your sample data has AGE in the relevant range were discussing. – Sirius Mar 14 '21 at 21:41
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/229917/discussion-between-computernoob-and-sirius). – ComputerNoob Mar 15 '21 at 00:24

1 Answers1

-1

There are lots of questions on here about fitting stratified (G)LMs. Here's one way.

## convert AGE back to numeric:
data.clean <- transform(data.clean, AGE=as.factor(as.character(AGE)))
fits <- lme4::lmList(COMPLICATION~AGE|BYDECADE, 
                 data = data.clean, 
                 family = binomial)
Ben Bolker
  • 211,554
  • 25
  • 370
  • 453