如何使用 `stat_poly_eq(method= "lm")` 获取 geom_smooth 相关图标签中的置信区间 (95%) 值?

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

我正在计算数据的相关图。但是,我需要相关系数的 CI 95% 值。我尝试在 stat(conf.int.label) 代码之后添加如标签中所示的代码:

Kan.PS_moc_plot <-Fnlcor_df%>%
  ggplot(aes(x= PSKannada, y= MOC))+
  geom_smooth(method = "lm",se = TRUE, alpha = 0.5)+
  geom_point(size =2)+
  stat_poly_eq(method = "lm",
               aes(label= paste(after_stat(rr.label), after_stat(p.value.label), after_stat(conf.int.label), sep = "*\",\"*")), label.x = "right", label.y = "bottom",
               size=4, family = "Palatino") + 
  labs(y=" MOC Median RT diffecrence (sec)", x="Kannada Phoneme Segmenation")+  
  facet_wrap(.~paste("Age Range:", AgeRange) + 
               paste("Group:", Group)) + 
  theme_classic() 

Kan.PS_moc_plot

但是我收到了这个错误

geom_smooth()` using formula = 'y ~ x' Error in `stat_poly_eq()`: ! Problem while mapping stat to aesthetics. ℹ Error occurred in the 3rd layer. Caused by error in `after_stat()`: ! object 'conf.int.label' not found Run `rlang::last_trace()` to see where the error occurred.

如何获得CI 95%值? “se = TRUE”只会在图表上绘制 CI 值阴影,但我需要报告 CI 值以及 R 平方值

提前感谢您的及时帮助!

r ggplot2 correlation confidence-interval
1个回答
0
投票

stat_poly_eq
不会计算每个系数的置信区间(如果您遵循
debug
中的小插图,您可以使用
?stat_poly_eq
模式来显示其中的内容)。我认为最顺利的方法是在外部计算利息系数并添加:

library(tidyverse)
library(ggpmisc)

model <- iris |>
  lm(Sepal.Length ~ Sepal.Width, data = _) 

coef(model)['Sepal.Width']
#> Sepal.Width 
#>  -0.2233611

coef_label <- model |> 
  confint() |> 
  _['Sepal.Width',] |> 
  sprintf("%.2f", x = _) |> 
  paste(collapse = " to ") |> 
  paste0("paste(beta[1], \" = ", sprintf("%.2f", coef(model)['Sepal.Width']), " (", x = _, ")\")")

iris |>
  ggplot(aes(Sepal.Width, Sepal.Length)) +
  geom_point() +
  geom_smooth(method = "lm") +
  stat_poly_eq(
    method = "lm",
    aes(label = paste(
      after_stat(rr.label),
      after_stat(p.value.label),
      coef_label,
      sep = "*\", \"*"
    )),
    label.x = "right", label.y = "bottom", size = 4
  )
#> `geom_smooth()` using formula = 'y ~ x'

编辑答案 - 使用
geom_text

您可以对每个组执行相同的操作,并使用

geom_text
将其绘制在多面图上。为了清楚起见,我删除了
stat_poly_eq
(因为它们很难不重叠,并且它们在面中不能完全粘贴在一起)

library(tidyverse)
library(gapminder)

gap_data <- gapminder |> 
  filter(continent != "Oceania", year == 2007) |> 
  mutate(gdpPercap = gdpPercap / 1000)

coef_labels <- gap_data |>
  nest(data = -continent) |>
  mutate(model = map(data, \(data) lm(lifeExp ~ gdpPercap, data = data)),
         coef_label = map_chr(model, \(model) {

           # This code is the label-pasting code from above
           # Creates a separate label per facet

           model |>
             confint() |>
             _['gdpPercap', ] |>
             sprintf("%.2f", x = _) |>
             paste(collapse = " to ") |>
             paste0("paste(beta[1], \" = ",
                    sprintf("%.2f", coef(model)['gdpPercap']),
                    " (", x = _, ")\")")

         })) |> 
  select(continent, coef_label)

gap_data |> 
  ggplot(aes(gdpPercap, lifeExp)) +
  geom_point() +
  geom_smooth(method = "lm") +
  geom_text(data = coef_labels,
            aes(x = Inf, y = -Inf, label = coef_label),
            hjust = 1,
            vjust = -1,
            parse = TRUE) +
  facet_wrap(~ continent, scales = "free") +
  xlab("GDP per capita ($1,000s)")
#> `geom_smooth()` using formula = 'y ~ x'

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