R:为什么我的 glm 的预测值没有形成曲线?

问题描述 投票:0回答:1

我有一个脚本,旨在创建一个 glm 并使用它来标准化 0 和 1 之间的数据集,之后我制作一个图表来显示关系。我一直在对多个数据集执行此操作,并且线条始终是弯曲的(如第一张图),但对于这个特定的数据集,曲线只是 3 条直线(第二张图)。我猜这与

newdata
中的
predict
有关,但我不确定。

我的代码:

# turn off scientific notation
options(scipen = 999)

# recreating the data
IV_BP <- structure(list(Breakpoints = c("Min", "BP1", "BP2", "BP3", "BP4", "Max"),
                        SES = c(-1.8, -0.3, -0.1, 0.1, 0.3, 0.8),
                        Normalised_value = c(0,0.2, 0.4, 0.6, 0.8, 1)),
                   class = "data.frame", row.names = c(NA, -6L))

IV_df <- structure(list(SES = c(-0.006, 0.078, 0.028, -0.066, 0.041, -0.025, 
                                0.006, -0.021, -0.013, -0.145, -0.065, 0.026, 0.068, -0.22, 0.138, 
                                0.019, 0.174, 0.107, 0.339, 0.219, 0.093, -0.057, -0.19, 0.01, 
                                0.085, -0.011, -0.075, -0.113, -0.019, 0.141, -0.045, -0.258, 
                                -0.02, -0.178, -0.142, -0.067, 0.1, -0.155, 0.007, -0.18, -0.258, 
                                -0.497)), class = "data.frame", row.names = c(NA, -42L))

# make glm
glmfit <- glm(Normalised_value~SES,data=IV_BP,family = quasibinomial())

# use glm to transform values
IV_df$CC_Transformed <- predict(glmfit,newdata=IV_df,type="response")

# make a graph
plot(IV_BP$SES, IV_BP$Normalised_value,
     xlab = "Socioeconomic Status Index Score",
     ylab = "Normalised Values",
     xlim = c(-2, 2),
     pch = 19,
     col = "blue",
     panel.first =
       c(abline(h = 0, col = "lightgrey"),
         abline(h = 0.2, col = "lightgrey"),
         abline(h = 0.4, col = "lightgrey"),
         abline(h = 0.6, col = "lightgrey"),
         abline(h = 0.8, col = "lightgrey"),
         abline(h = 1, col = "lightgrey"),
         lines(-2:2,predict(glmfit,newdata=data.frame(SES=-2:2),type="response"),
      col = "lightblue",
      lwd = 5)))
r plot glm predict
1个回答
0
投票

您的 x 值

-2:2
分辨率不足以为您提供曲线。使用
seq
以 0.1 为步长增加分辨率。

pred_df <- data.frame(SES = seq(-2, 2, by = 0.1))
pred_df$CC_Transformed <- predict(glmfit, newdata = pred_df, type = "response")

# make a graph
plot(IV_BP$SES, IV_BP$Normalised_value,
     xlab = "Socioeconomic Status Index Score",
     ylab = "Normalised Values",
     xlim = c(-2, 2),
     pch = 19,
     col = "blue",
     panel.first =
       c(abline(h = 0, col = "lightgrey"),
         abline(h = 0.2, col = "lightgrey"),
         abline(h = 0.4, col = "lightgrey"),
         abline(h = 0.6, col = "lightgrey"),
         abline(h = 0.8, col = "lightgrey"),
         abline(h = 1, col = "lightgrey")
       ))
lines(CC_Transformed ~ SES, data = pred_df, col = "lightblue", lwd = 5)

创建于 2023-10-28,使用 reprex v2.0.2

© www.soinside.com 2019 - 2024. All rights reserved.