均值计算
- 自编函数进行均值和五数计算
function(x,na.omit=FALSE){
if(na.omit)
x <- x[!is.na(x)]
m <- mean(x)
sd <- sd(x)
min <- min(x)
max <- max(x)
Q <- quantile(x)
return(c(mean=m,std=sd,min=min,max=max,quantile=Q))
}
attach(df)
stata <- c("X","Y","D") #写入需要进行计算的变量
outcome <- sapply(df[stata],describestat)
detach(df)
- 自编函数进行均值和标准差计算
function(x,parametric=TRUE,print=FALSE){
if(parametric){
center <- mean(x);spread <- sd(x)
}else{
center <- median(x);spread <- mad(x)
}
if(print & parametric){
cat("Median=",center,"\n","MAD=",spread,"\n")
}
result <- list(center=center,spread=spread)
return(result)
}
- 使用R自带包进行均值计算
单因素logistic回归
logitUniVar <- function(dat, group, var, digit = 3){
formu <- as.formula(paste0(group, " ~ ", var))
dat[[group]] <- as.factor(dat[[group]])
subgroup <- levels(as.factor(dat[[group]]))
subgroup1 <- paste0(subgroup[2], " vs ", subgroup[1])
fit <- glm(formu, data = dat, family = binomial())
unisum <- summary(fit)
OR <- exp(coef(fit))[2]
OR <- round(OR, digit)
ci <- exp(confint(fit))[2,]
ci <- round(ci, digit)
cito <- paste0(ci[1], " - ", ci[2])
p <- unisum$coefficients[2, "Pr(>|z|)"]
p <- ifelse(p < 0.001, "< 0.001", round(p, 3))
var1 <- names(exp(coef(fit)))[2]
result <- c(var1, group,subgroup1, OR, cito, p)
names(result) <- c("var", "group","subgroup", "OR", "95%CI", "p.val")
return(result)
}
#y1 进行分析
multivar <- c("X", "Y", "D")
logitRes <- lapply(multivar, function(x) logitUniVar(df, group = "y1", var = x))
logitResDat <- do.call(rbind, logitRes)
logitResDat