1

According to my understanding fixed effects gives different intercepts for different clusters and so does random effects model with fixed slope and varying intercept. This is the sample data used. enter image description here

I have used dummies for fixed effects

data_hos<-read.csv("hospital_data.csv")
print(data_hos)
library("dplyr")
data_hos<-data_hos %>% 
  mutate(ABC = case_when(
    HOSPITAL=="ABC" ~ 'ABC',
    HOSPITAL=="CDE" ~ '',
    HOSPITAL=="FGH" ~ '',
        ))
data_hos<-data_hos %>% 
  mutate(CDE = case_when(
    HOSPITAL=="ABC" ~ '',
    HOSPITAL=="CDE" ~ 'CDE',
    HOSPITAL=="FGH" ~ '',
  ))
data_hos<-data_hos %>% 
  mutate(FGH = case_when(
    HOSPITAL=="ABC" ~ '',
    HOSPITAL=="CDE" ~ '',
    HOSPITAL=="FGH" ~ 'FGH',
  ))
print(data_hos)
model_ols<-lm(SURVIVALRATE~SEVEREITY+ABC+CDE,data=data_hos)
summary(model_ols)
library(ggplot2)
data_hos$predlm=predict(model_ols)
head(data_hos)
ggplot(data_hos,aes(x= SEVEREITY, y = SURVIVALRATE, color = as.factor(HOSPITAL)))+
  geom_point() +
  geom_line(aes(y = predlm), size = 1)

with the output fit as enter image description here

And then for the random effects I have used the lmer function from lme4 package

data_hos<-read.csv("hospital_data.csv")
library("lme4")
model_mixed<-lmer(SURVIVALRATE ~ 1+SEVEREITY+(1|HOSPITAL),data=data_hos)
summary(model_mixed)
library(ggplot2)
data_hos$predlmer=predict(model_mixed)
ggplot(data_hos,aes(x= SEVEREITY, y = SURVIVALRATE, color = as.factor(HOSPITAL)))+
 geom_point() +
 geom_line(aes(y = predlmer), size = 1)

with the output fit as enter image description here

What is the difference between these two as they both seem to do the same thing. Even after reading many articles and posts online, I could never understand it entirely.

  • 1
    3 is too few groups to use for a random effect. The smallest number I've seen recommended is 6, but claims of 15-30 also exist. So in this case you're better off with a fixed-effects model. – mkt Sep 21 '22 at 08:52
  • 1
    You'll find a few threads about this on this site if you search around. Here's one that mentions 6 groups though it's not the focus: https://stats.stackexchange.com/questions/492426/ramifications-of-small-unbalanced-group-sizes-small-number-of-groups-for-fixe/492516#492516 – mkt Sep 21 '22 at 08:53

0 Answers0