用ggpmisc显示nls模型的方程 [英] Showing equation of nls model with ggpmisc

查看:184
本文介绍了用ggpmisc显示nls模型的方程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

R 程序包 ggpmisc 可用于显示 lm 模型和 poly 模型放在 ggplot2 请参阅此处以供参考)。不知道如何使用 ggmisc ggplot2 上显示 nls >。

  library(ggpmisc)
args< - list(formula = y〜k * e) ^ $
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(公式= mpg〜k * e ^ wt,
start = list(k = 1,e = 2),
data = mtcars)

nlsParams < -
nlsFit $ m $ getAllPars()

nlsEqn < -
替换(italic(y)== k%。%e ^ italic(x),
list(k =格式(nlsParams ['k'],digits = 4),
e =格式(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 package ggpmisc can be used to show equation of lm model and poly model on ggplot2 (See here for reference). Wonder how to show nls model equation results on ggplot2 using ggmisc. Below is my 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)

解决方案

Inspired by the post you linked. Use geom_text to add the label after extracting parameters.

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)

这篇关于用ggpmisc显示nls模型的方程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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