#载入数据
data<-read.csv("分娩镇痛与7周产后预后评分.csv")
str(data)
data$Score <- as.numeric(data$Score)
data$LaborAnalgesia <- as.factor(data$LaborAnalgesia)
data$Delivery <- as.factor(data$Delivery)
data$ID <- as.factor(data$ID)
#简单卡方检验
data$Score_group <- ifelse(data$Score<=13,0,1)
table(data$Score_group,data$LaborAnalgesia)
s=chisq.test(data$Score_group,data$LaborAnalgesia,correct = TRUE)
s
s$expected
#分层卡方分析 【按Delivery分层】
table(data$Score_group,data$LaborAnalgesia,data$Delivery)
mantelhaen.test(data$Score_group,data$LaborAnalgesia,data$Delivery)
#【广义线性模型】 【当成因变量】
fit <- glm(Score_group ~ LaborAnalgesia+Delivery,family = binomial(link = "logit"), data)
summary(fit)
#考虑交互作用
fit2 <- glm(Score_group ~ LaborAnalgesia*Delivery,family = binomial(link = "logit"), data)
summary(fit2)
# 广义线性模型fit的R-Square
library(rsq)
rsq(fit, #模型
adj=FALSE #是否矫正R2
,type=c("lr"))
rsq(fit2,adj=FALSE,type=c("lr"))
#【线性混合模型】【分娩方式Delivery当成随机效应】
library(lme4)
lmer1 <- lmer(Score_group ~ LaborAnalgesia+(1|Delivery),data )
summary(lmer1)
rsq(lmer1,adj=FALSE,type=c("lr"))