试图注释带有回归的图会导致r降低

发布于 2025-02-11 20:54:59 字数 810 浏览 2 评论 0原文

我试图通过BETA,标准错误和P值直接从我保存的模型摘要中添加脚注。但是,它一直告诉我解析文本中存在意外错误。任何帮助将不胜感激!

确切错误: 解析错误(文本=文本[[i])):: 1:26:意外输入 1:“年龄的主要影响:'$ ^

最小可重复的例子:

id<-rep(1:50)
tst<-c(sample(7:9,50, replace = T))

  
mydf<-data.frame(id,tst)

mydf$age<-sample(40:90,50, replace = T)
mydf$bmi<-sample(20:30,50, replace = T)
mydf$sex<-sample(1:2,50, replace = T)


##Overall model##
model <- lm( tst ~  age*sex + bmi , data = mydf)
summary(model)

model.df<-ggpredict(model, terms = c("age", "sex"))


model.plot<-plot(model.df)+theme(legend.position="none")+
  theme(plot.title = element_text(hjust = 0.5))+
  annotate("text", x = 0, y = 0.05, parse = TRUE, size = 4, 
           label = " 'Main effect of age: ' $\beta ==  %.2g ",
           coef(model)[2])

(model.plot)


I am trying to add a footnote to the bottom of my plot with betas, standard errors, and p values directly from the model summary I saved. However, it keeps telling me there is an unexpected error in the parse text. Any help would be greatly appreciated!

exact error:
Error in parse(text = text[[i]]) : :1:26: unexpected input
1: 'Main effect of age: ' $
^

minimal reproducible example:

id<-rep(1:50)
tst<-c(sample(7:9,50, replace = T))

  
mydf<-data.frame(id,tst)

mydf$age<-sample(40:90,50, replace = T)
mydf$bmi<-sample(20:30,50, replace = T)
mydf$sex<-sample(1:2,50, replace = T)


##Overall model##
model <- lm( tst ~  age*sex + bmi , data = mydf)
summary(model)

model.df<-ggpredict(model, terms = c("age", "sex"))


model.plot<-plot(model.df)+theme(legend.position="none")+
  theme(plot.title = element_text(hjust = 0.5))+
  annotate("text", x = 0, y = 0.05, parse = TRUE, size = 4, 
           label = " 'Main effect of age: ' $\beta ==  %.2g ",
           coef(model)[2])

(model.plot)


如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月亮邮递员 2025-02-18 20:54:59

似乎解析您的语法进行解析是错误的。另外,您的代码会将文本添加到每个方面 - 不确定这是否是预期的结果(如果是这样,只需使用parse = falsepaste0(...)从下面的注释中表达

library(ggiraphExtra)
library(ggplot2)
set.seed(1234)
mydf <- data.frame(
    id = 1:50,
    tst = sample(7:9, 50, replace = T),
    age = sample(40:90,50, replace = T),
    bmi = sample(20:30,50, replace = T),
    sex = sample(1:2,50, replace = T)
    )

##Overall model##
model <- lm( tst ~  age*sex + bmi , data = mydf)
summary(model)
#> 
#> Call:
#> lm(formula = tst ~ age * sex + bmi, data = mydf)
#> 
#> Residuals:
#>      Min       1Q   Median       3Q      Max 
#> -1.24030 -0.67286 -0.07152  0.62489  1.27281 
#> 
#> Coefficients:
#>             Estimate Std. Error t value Pr(>|t|)   
#> (Intercept)  4.99309    1.66745   2.994  0.00446 **
#> age          0.01975    0.02389   0.827  0.41274   
#> sex          1.21860    0.95986   1.270  0.21077   
#> bmi          0.06602    0.03805   1.735  0.08955 . 
#> age:sex     -0.01852    0.01532  -1.209  0.23307   
#> ---
#> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#> 
#> Residual standard error: 0.7507 on 45 degrees of freedom
#> Multiple R-squared:  0.125,  Adjusted R-squared:  0.04721 
#> F-statistic: 1.607 on 4 and 45 DF,  p-value: 0.189
model.df <- ggPredict(model, terms = c("age", "sex"))
model.plot <- model.df + 
    theme(legend.position="none", 
        plot.title = element_text(hjust = 0.5)) +
    labs(caption = paste0(
               "Main effect of age: β = ", 
               sprintf("%.2g", coef(model)[2])))
model.plot

出来

Seems like parsing your syntax for parsing is wrong. Also, your code would add the text to each facet - not sure if that was the intended outcome (if so, just use parse=FALSE and the paste0(...) expression from below for your annotation. If you wish a global footnote, you could a caption like so:

library(ggiraphExtra)
library(ggplot2)
set.seed(1234)
mydf <- data.frame(
    id = 1:50,
    tst = sample(7:9, 50, replace = T),
    age = sample(40:90,50, replace = T),
    bmi = sample(20:30,50, replace = T),
    sex = sample(1:2,50, replace = T)
    )

##Overall model##
model <- lm( tst ~  age*sex + bmi , data = mydf)
summary(model)
#> 
#> Call:
#> lm(formula = tst ~ age * sex + bmi, data = mydf)
#> 
#> Residuals:
#>      Min       1Q   Median       3Q      Max 
#> -1.24030 -0.67286 -0.07152  0.62489  1.27281 
#> 
#> Coefficients:
#>             Estimate Std. Error t value Pr(>|t|)   
#> (Intercept)  4.99309    1.66745   2.994  0.00446 **
#> age          0.01975    0.02389   0.827  0.41274   
#> sex          1.21860    0.95986   1.270  0.21077   
#> bmi          0.06602    0.03805   1.735  0.08955 . 
#> age:sex     -0.01852    0.01532  -1.209  0.23307   
#> ---
#> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#> 
#> Residual standard error: 0.7507 on 45 degrees of freedom
#> Multiple R-squared:  0.125,  Adjusted R-squared:  0.04721 
#> F-statistic: 1.607 on 4 and 45 DF,  p-value: 0.189
model.df <- ggPredict(model, terms = c("age", "sex"))
model.plot <- model.df + 
    theme(legend.position="none", 
        plot.title = element_text(hjust = 0.5)) +
    labs(caption = paste0(
               "Main effect of age: β = ", 
               sprintf("%.2g", coef(model)[2])))
model.plot

Created on 2022-06-30 by the reprex package (v2.0.1)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文