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.

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)
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)
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.

