首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用ggpmisc显示nls模型的方程

用ggpmisc显示nls模型的方程
EN

Stack Overflow用户
提问于 2016-08-01 00:21:24
回答 2查看 1.1K关注 0票数 15

R软件包ggpmisc可用于在ggplot2上显示lm模型方程和poly模型方程(参考See here)。我想知道如何使用ggpmiscggplot2上显示nls模型方程结果。下面是我的MWE。

代码语言:javascript
复制
library(ggpmisc)
args <- list(formula = y ~ k * e ^ x,
             start = list(k = 1, e = 2))
ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  stat_fit_augment(method = "nls",
                   method.args = args)
EN

回答 2

Stack Overflow用户

发布于 2016-08-07 09:29:03

受到你链接的帖子的启发。提取参数后,使用geom_text添加标签。

代码语言:javascript
复制
nlsFit <-
  nls(formula = mpg ~ k * e ^ wt,
      start = list(k = 1, e = 2),
      data = mtcars)

nlsParams <-
  nlsFit$m$getAllPars()

nlsEqn <-
  substitute(italic(y) == k %.% e ^ italic(x), 
             list(k = format(nlsParams['k'], digits = 4), 
                  e = format(nlsParams['e'], digits = 2)))

nlsTxt <-
  as.character(as.expression(nlsEqn))

ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  stat_fit_augment(method = "nls",
                   method.args = args) + 
  geom_text(x = 5, y = 30, label = nlsTxt, parse = TRUE)
票数 4
EN

Stack Overflow用户

发布于 2021-02-05 01:31:41

在这里,我使用当前的CRAN ggpmisc (v0.3.8)显示了nls和使用ggpmisc添加到绘图中的组。这是片段的变体/修改,其中'stat_fit_tidy()‘使用michaelis-menten拟合,找到here。输出如下所示:

代码语言:javascript
复制
library(tidyverse)
library(tidymodels)
library(ggpmisc)

my_exp_formula <-  y ~ a * exp(b*x-0)
# if x has large values (i.e. >700), subtract the minimum
# see https://stackoverflow.com/a/41108403/4927395

#example with nls, shows the data returned
o <- nls(1/rate ~ a * exp(b*conc-0), data = Puromycin, start = list(a = 1, b = 2))
o
tidy(o)

ggplot(Puromycin, aes(conc, 1/rate, colour = state)) +
  geom_point() +
  geom_smooth(method = "nls", 
              formula = my_exp_formula,
              se = FALSE) +
  stat_fit_tidy(method = "nls", 
                method.args = list(formula = my_exp_formula),
                label.x = "right",
                label.y = "top",
                aes(label = paste("a~`=`~", signif(stat(a_estimate), digits = 3),
                                  "%+-%", signif(stat(a_se), digits = 2),
                                  "~~~~b~`=`~", signif(stat(b_estimate), digits = 3),
                                  "%+-%", signif(stat(b_se), digits = 2),
                                  sep = "")),
                parse = TRUE)
ggsave("exp plot.png")
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38686029

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档