带有output.type ="numeric"的每个方面的系数.在ggpmisc :: stat_poly_eq中 [英] Coefficients per facet with output.type="numeric" in ggpmisc::stat_poly_eq

查看:294
本文介绍了带有output.type ="numeric"的每个方面的系数.在ggpmisc :: stat_poly_eq中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ggpmisc::stat_poly_eq具有选项output.type = "numeric",该选项允许获取拟合模型参数的估计值.下面是我尝试将其与facet_wrap一起使用的尝试.我在每个方面都得到了不同的,但是在两个方面中,系数是相同的.我做错了什么,还是一个错误?

ggpmisc::stat_poly_eq has an option output.type = "numeric" allowing to get the estimates of the parameters of the fitted model. Below is my attempt to use it with facet_wrap. I get a different per facet but the coefficients are the same in the two facets. Do I do something wrong, or is it a bug?

library(ggpmisc)

set.seed(4321)
x <- 1:100
y <- (x + x^2 + x^3) + rnorm(length(x), mean = 0, sd = mean(x^3) / 4)
my.data <- data.frame(x = x, 
                      y = y,
                      group = c("A", "B"))
my.data[my.data$group=="A",]$y <- my.data[my.data$group=="A",]$y + 200000

formula <- y ~ poly(x, 1, raw = TRUE)

myformat <- "Intercept: %s\nSlope: %s\nR²: %s"
ggplot(my.data, aes(x, y)) + 
  facet_wrap(~ group) + 
  geom_point() +
  geom_smooth(method = "lm", formula = formula) +
  stat_poly_eq(formula = formula, output.type = "numeric",
               mapping = aes(label = 
                               sprintf(myformat,
                                       formatC(stat(coef.ls)[[1]][[1, "Estimate"]]),
                                       formatC(stat(coef.ls)[[1]][[2, "Estimate"]]),
                                       formatC(stat(r.squared))))) 

我们必须掌握面板编号.奇怪的是,formatC(stat(as.integer(PANEL)))返回每个面的面板编号:

We have to catch the panel number. It is strange that formatC(stat(as.integer(PANEL))) returns the panel number per facet:

,但是formatC(stat(coef.ls)[[stat(as.integer(PANEL))]][[1, "Estimate"]])不起作用,因为在这里PANEL = c(1,2).

but however formatC(stat(coef.ls)[[stat(as.integer(PANEL))]][[1, "Estimate"]]) does not work, because here PANEL = c(1,2).

推荐答案

好,我知道了.

ggplot(my.data, aes(x, y)) + 
  facet_wrap(~ group) + 
  geom_point() +
  geom_smooth(method = "lm", formula = formula) +
  stat_poly_eq(
    formula = formula, output.type = "numeric",
    mapping = aes(label = 
                    sprintf(myformat,
                            c(formatC(stat(coef.ls)[[1]][[1, "Estimate"]]), 
                              formatC(stat(coef.ls)[[2]][[1, "Estimate"]])),
                            c(formatC(stat(coef.ls)[[1]][[2, "Estimate"]]), 
                              formatC(stat(coef.ls)[[2]][[2, "Estimate"]])),
                            formatC(stat(r.squared))))) 

这篇关于带有output.type ="numeric"的每个方面的系数.在ggpmisc :: stat_poly_eq中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆