gpt4 book ai didi

r - 用 ggpmisc 显示 nls 模型的方程

转载 作者:行者123 更新时间:2023-12-03 22:39:21 24 4
gpt4 key购买 nike

R包裹ggpmisc可以用来表示lm的方程模型和 poly型号在 ggplot2 ( See here 供引用)。我想知道如何显示 nls ggplot2 上的模型方程结果使用 ggpmisc .下面是我的 MWE。

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)

最佳答案

灵感来自你链接的帖子。使用 geom_text提取参数后添加标签。

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)

关于r - 用 ggpmisc 显示 nls 模型的方程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38686029/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com