0

我已经使用“预测”为我创建的线性模型(lm)找到了一条拟合线。因为 lm 只建立在 2 个数据点上并且需要有一个正斜率,所以我强迫它通过原点 (0,0)。我还根据每个数据点的观察次数对函数进行了加权。

问题 1:(已解决 - 参见 @Gregor 的评论)为什么当 B 的基础观察值较少时,预测线与我的第二个数据点 (B) 比我的第一个数据点 (A) 更接近?加权模型时我在这里编码错误吗?

问题 2:现在绘制 GLM (link=logit),但我仍然如何强制它通过 0,0?我尝试formula = y~0+x在几个地方添加,但似乎都不起作用。

M <- data.frame("rate" = c(0.4643,0.2143), "conc" = c(300,6000), "nr_dead" = c(13,3), "nr_surv" = c(15,11), "region" = c("A","B"))
M$tot_obsv <- (M$nr_dead+M$nr_surv)
M_conc <- M$conc
M_rate <- M$rate
M_tot_obsv <- M$tot_obsv
#**linear model of data, force 0,0 intercept, weighted by nr. of observations of each data point.**
M_lm <- lm(data = M, rate~0+conc, weights = tot_obsv)
#**plot line using "predict" function**
x_conc <-c(600, 6700)
y_rate <- predict(M_lm, list(conc = x_conc), weights = tot_obsv, type = 'response') 
plot(x = M$conc, y = M$rate, pch = 16, ylim = c(0, 0.5), xlim = c(0,7000), xlab = "conc", ylab = "death rate")
lines(x_conc, y_rate, col = "red", lwd = 2)

#**EDIT 1:**

M_glm <- glm(cbind(nr_dead, nr_surv) ~ (0+conc), data = M, family = "binomial")

#*plot using 'predict' function*
binomial_smooth <- function(formula = (y ~ 0+x),...) {
    geom_smooth(method = "glm", method.args = list(family = "binomial"), formula = (y ~ 0+x), ...)
}
tibble(x_conc = c(seq(300, 7000, 1), M$conc), y_rate = predict.glm(M_glm, list(conc = x_conc), type = "response")) %>% left_join(M, by = c('x_conc' = 'conc')) %>% 
ggplot(aes(x = x_conc, y = y_rate)) + xlab("concentration") + ylab("death rate") +
geom_point(aes(y = rate, size = tot_obsv)) + binomial_smooth(formula = (y ~ 0+x)) + theme_bw() 
4

0 回答 0