使用 dplyr 从多个回归模型中提取斜率
Extracting slopes from several regression models using dplyr
我想使用 dplyr 为每个人和每种药物(两者都是因子变量)拟合多个模型,但出现错误,我不太确定哪里出了问题。我想提取每个模型的斜率来计算个体之间拟合模型的成对关系的平均斜率。
df.dr <- data.frame(
drug = factor(rep(rep(LETTERS[1:4], each = 5), 4)),
individual = factor(rep(letters[1:4], each = 20)),
dose = rep(c(10,5,1,0.5,0.1), times = 16),
viability = runif(80, min = 50, max = 200))
dfDrugInd = df.dr %>% group_by(drug, individual) %>%
do(fitAll = lm(viability ~ ., data = .))
dfDrugIndSlope = tidy(dfDrugInd, fitAll)$estimate[2,1]
mean(dfDrugIndSlope)
我还是不太明白你为什么要这样进行回归分析(一个一个的进行,然后平均系数),下面的代码应该可以满足你的需求:
但是你的问题似乎是一个 mixed-effect
模型,你可能想使用 lm4
包来实现你的结果。
df.dr <- data.frame(
drug = factor(rep(rep(LETTERS[1:4], each = 5), 4)),
individual = factor(rep(letters[1:4], each = 20)),
dose = rep(c(10,5,1,0.5,0.1), times = 16),
viability = runif(80, min = 50, max = 200))
# split the dataset by group
df.sp <- split(df.dr, df.dr$drug)
# run separate `lm` on each group and store the results in a list
r <- lapply(df.sp, function(x) do.call("lm", list(viability ~ individual + dose, x)))
# extract the coefficient of target variable, say `individualc`
indi.cof <- sapply(r, function(x)x$coefficients[["individualc"]])
# get the mean
mean(indi.cof)
我想使用 dplyr 为每个人和每种药物(两者都是因子变量)拟合多个模型,但出现错误,我不太确定哪里出了问题。我想提取每个模型的斜率来计算个体之间拟合模型的成对关系的平均斜率。
df.dr <- data.frame(
drug = factor(rep(rep(LETTERS[1:4], each = 5), 4)),
individual = factor(rep(letters[1:4], each = 20)),
dose = rep(c(10,5,1,0.5,0.1), times = 16),
viability = runif(80, min = 50, max = 200))
dfDrugInd = df.dr %>% group_by(drug, individual) %>%
do(fitAll = lm(viability ~ ., data = .))
dfDrugIndSlope = tidy(dfDrugInd, fitAll)$estimate[2,1]
mean(dfDrugIndSlope)
我还是不太明白你为什么要这样进行回归分析(一个一个的进行,然后平均系数),下面的代码应该可以满足你的需求:
但是你的问题似乎是一个 mixed-effect
模型,你可能想使用 lm4
包来实现你的结果。
df.dr <- data.frame(
drug = factor(rep(rep(LETTERS[1:4], each = 5), 4)),
individual = factor(rep(letters[1:4], each = 20)),
dose = rep(c(10,5,1,0.5,0.1), times = 16),
viability = runif(80, min = 50, max = 200))
# split the dataset by group
df.sp <- split(df.dr, df.dr$drug)
# run separate `lm` on each group and store the results in a list
r <- lapply(df.sp, function(x) do.call("lm", list(viability ~ individual + dose, x)))
# extract the coefficient of target variable, say `individualc`
indi.cof <- sapply(r, function(x)x$coefficients[["individualc"]])
# get the mean
mean(indi.cof)