寻找一些帮助以固定截距拟合二项式模型。我已经看到了这个问题,通过从回归中减去显式截距,然后拟合无截距的模型,成功地将线性模型涵盖在内:
intercept <- 1.0
fit <- lm(I(x - intercept) ~ 0 + y, lin)
summary(fit)
或通过指定偏移量:
m(x ~ y +0 + offset(rep(1, nrow(lin))),
但是我不知道如何通过二项式回归来做到这一点。下面的示例代码:
library(ggplot2)
library(lme4)
library(dplyr)
library(gather)
# simulate dummy dataframe
x <- data.frame(time = c(1, 1, 1, 1, 1, 1,1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2,3, 3, 3, 3, 3, 3, 3, 3, 3,4, 4, 4, 4, 4, 4, 4, 4, 4),
type = c('a', 'a', 'a', 'b', 'b', 'b','c','c','c','a', 'a', 'a', 'b', 'b', 'b','c','c','c','a', 'a', 'a', 'b', 'b', 'b','c','c','c','a', 'a', 'a', 'b', 'b', 'b','c','c','c'), randef=c('aa','ab','ac','ba','bb','bc','ca','cb','cc','aa','ab','ac','ba','bb','bc','ca','cb','cc','aa','ab','ac','ba','bb','bc','ca','cb','cc','aa','ab','ac','ba','bb','bc','ca','cb','cc'),
surv = sample(x = 1:200, size = 36, replace = TRUE),
nonsurv= sample(x = 1:200, size = 36, replace = TRUE))
# convert to survival and non survival into individuals following
# https://stackoverflow.com/questions/51519690/convert-cbind-format-for- binomial-glm-in-r-to-a-dataframe-with-individual-rows
x_long <- x %>%
gather(code, count, surv, nonsurv)
# function to repeat a data.frame
x_df <- function(x, n){
do.call('rbind', replicate(n, x, simplify = FALSE))
}
# loop through each row and then rbind together
x_full <- do.call('rbind',
lapply(1:nrow(x_long),
FUN = function(i)
x_df(x_long[i,], x_long[i,]$count)))
# create binary_code
x_full$binary <- as.numeric(x_full$code == 'surv')
### binomial glm with interaction between time and type:
summary(fm2<-glm(binary ~ time*type, data = x_full, family = "binomial"))
### plot glm in ggplot2
ggplot(x_full, aes(x = time, y = as.numeric(x_full$binary), fill= x_full$type)) +
geom_smooth(method="glm", aes(color = factor(x_full$type)), method.args = list(family = "binomial"))
我想做的是将截距强制设置为1(即所有在第一个时间点幸存),就像一条生存曲线一样。在此示例中,指定偏移似乎无效,因为它是二项式概率,所以指定1或0无效,并且在第一时间点存活的个体与未存活的个体的数量因因素而异)
堆栈溢出,我从您那里学到的知识比任何我的本科班或研究生导师都多。关于这个有什么建议吗?