繁体   English   中英

用 ggplot2 在 R 中绘制 GLM 的预测概率

[英]Plotting predicted probability of GLM in R with ggplot2

我想为平均模型中不同土地利用类别的不同预测概率创建一个包含多条线的图。

我现在有两个类别的代码(我总共要绘制 4 个):

Cat1.res <- invlogit(av.model2022.hec$coefficients["full","(Intercept)"]
                     + av.model2022.hec$coefficients["full","Cat.1"]*seq(min(df2022hec$Cat.1), max(df2022hec$Cat.1),(max(df2022hec$Cat.1) - min(df2022hec$Cat.1))/50))

Cat9.res <- invlogit(av.model2022.hec$coefficients["full","(Intercept)"]
                     + av.model2022.hec$coefficients["full","Cat.9"]*seq(min(df2022hec$Cat.1), max(df2022hec$Cat.9),(max(df2022hec$Cat.9) - min(df2022hec$Cat.9))/50))

Cat1.seq <- seq(min(df2022hec$Cat.1), max(df2022hec$Cat.1),(max(df2022hec$Cat.1) - min(df2022hec$Cat.1))/50)

Cat9.seq <- seq(min(df2022hec$Cat.9), max(df2022hec$Cat.9),(max(df2022hec$Cat.9) - min(df2022hec$Cat.9))/50) 

plot(Cat9.seq, Cat9.res)
line(Cat1.seq, Cat1.res)

ggplot(df2022hec, aes(x=Cat9.seq, y=Cat9.res)) + 
  geom_line(aes(y = Cat1.res), color = "darkred") 

第一种绘图方法是使用 plot() 函数和 line() 函数。 然而,ggplot2 有更多的可能性使情节看起来不错,所以我更喜欢使用它。 但是,如果我运行最后一点编码,我会收到以下错误:

Error in `check_aesthetics()`:
! Aesthetics must be either length 1 or the same as the data (1277): y and x
Backtrace:
 1. base `<fn>`(x)
 2. ggplot2:::print.ggplot(x)
 4. ggplot2:::ggplot_build.ggplot(x)
 5. ggplot2 by_layer(function(l, d) l$compute_aesthetics(d, plot))
 6. ggplot2 f(l = layers[[i]], d = data[[i]])
 7. l$compute_aesthetics(d, plot)
 8. ggplot2 f(..., self = self)
 9. ggplot2:::check_aesthetics(evaled, n)

所以我想我需要找到一种方法来使我创建的 seq 和 res 对象的长度与我的数据相同? 这是唯一的问题吗? 如果是这样,我该怎么做?

提前致谢!

啊,我想我已经通过将值 50 更改为值 1276 来解决它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM