R ggplot2与R²和p的指数回归 [英] R ggplot2 exponential regression with R² and p

查看:383
本文介绍了R ggplot2与R²和p的指数回归的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在ggplot2中做指数回归。因此,首先我的匆匆:

  g < -  ggplot(data,aes(x = datax,y = datay),color = black)+ 
geom_point(shape = 1)+ stat_smooth(method ='nls',formula = y〜a * exp(b * x),aes(color ='Exponential'),se = FALSE)
g < - g + theme_classic()
g< - g + theme(panel.grid.major = element_blank())
g< - g + theme(panel.grid.minor =元素_line(color =black),
axis.line.y = element_line(color =black),$元素_blank())
g < - g + theme(axis.line.x = element_line b $ b panel.border = element_blank(),
panel.background = element_blank())
g < - g + labs(x =\\\
datax,y =datay\\\

g< - g + theme(axis.text.y = element_text(size = 14))
g< - g + theme(axis.text.x = element_text(size = 14))
g< -g + theme(axis.title.y = element_text(size = 18,vjust = 1))
g< -g + theme(axis.title.x = element_text(size = 18, vjust = 1))
g

这是我得到的图片





作为一名R初学者,我通过混合我的脚本和互联网来完成脚本。我总是得到以下错误:
$ b


In(函数(公式,data = parent.frame(),start,control = nls。控制(),:没有为某些参数指定起始值
将'a','b'初始化为'1',考虑指定'start'或者使用selfStart模型'

我还没有找到更好的方法去做指数图。



另外,我会喜欢将图形的颜色改为黑色并删除图例,我想在图形中有R²和p值(可能也是置信区间?)

解决方案

如果没有可复制的例子和很多问题,这并不容易回答
您确定您报告的消息是错误而不是警告吗?笔记本电脑,数据集虹膜,我得到了一个警告......



然而,你如何阅读关于R文档的页面,你应该提供通过参数start开始估算的初始值,以帮助找到收敛。如果你不提供它,nls()本身应该使用一些虚拟的默认值(在你的情况下,a和b被设置为1)。

你可以尝试像这样:

  g < -  ggplot(data,aes(x = datax,y = datay),color = (b = 1)),
公式= y〜a * exp(b * x),color ='black',se = FALSE)

你告诉R,情节的颜色是Exponential,我认为这样也行得通(我试着用R-base数据集'iris'工作)。
您可以注意到我传递了start参数作为传递给'method.args'的列表的一个元素:这是ggplot v2.0.0中的一个新特性。



希望这可以帮助

编辑
只是为了完整性,我将我在笔记本电脑上复制的代码与默认数据集:(请注意,它没有意义与这样的数据集指数拟合,但代码运行没有警告)

 库(ggplot2)
data('iris')
g1 < - ggplot(data = iris,aes(x = Sepal.Length,y = Sepal.Width))+
geom_point(color ='green')+ geom_smooth(method ='nls',
method.args = list(start = c(a = 1,b = 1)),se = FALSE,
formula = y〜 a * exp(b * x),color ='black')
g1


I am trying to do a exponential regression in ggplot2. So first my skript:

g <- ggplot(data, aes(x=datax, y=datay), color="black") +
 geom_point(shape=1) +  stat_smooth(method = 'nls', formula = y~a*exp(b*x), aes(colour = 'Exponential'), se = FALSE)
g <- g + theme_classic()
g <- g + theme(panel.grid.major=element_blank())
g <- g + theme(panel.grid.minor=element_blank())
g <- g + theme(axis.line.x=element_line(color="black"),
           axis.line.y=element_line(color="black"),
           panel.border=element_blank(),
           panel.background=element_blank())
g <- g + labs(x="\ndatax",y="datay\n")
g <- g + theme(axis.text.y=element_text(size=14))
g <- g + theme(axis.text.x=element_text(size=14))
g <- g + theme(axis.title.y=element_text(size=18,vjust=1))
g <- g + theme(axis.title.x=element_text(size=18,vjust=1))
g

This is the image that I got

As a R-beginner I did the script by mixing scripts of mine and the internet. I always get the following error:

"In (function (formula, data = parent.frame(), start, control = nls.control(), : No starting values specified for some parameters. Initializing ‘a’, ‘b’ to '1.'.Consider specifying 'start' or using a selfStart model"

I did not found a better way to do the exponential graph, yet.

In addition, I would like to change the color of the graph into black and delete the legend and I would like to have the R² and p value in the graph. (maybe as well the confidence intervals?)

解决方案

It's not easy to answer without a reproducible example and so many questions. Are you sure the message you reported is an error and not a warning instead? On my own laptop, with dataset 'iris', I got a warning...

However, how you can read on ?nls page on R documentation, you should provide through the parameter "start" an initial value for starting the estimates to help finding the convergence. If you don't provide it, nls() itself should use some dummy default values (in your case, a and b are set to 1).

You could try something like this:

g <- ggplot(data, aes(x=datax, y=datay), color="black") +
     geom_point(shape=1) +  stat_smooth(method = 'nls', 
     method.args = list(start = c(a=1, b=1)), 
     formula = y~a*exp(b*x), colour = 'black', se = FALSE)

You told R that the colour of the plot is "Exponential", I think that so is going to work (I tried with R-base dataset 'iris' and worked). You can notice that I passed the start parameter as an element of a list passed to 'method.args': this is a new feature in ggplot v2.0.0.

Hope this helps

Edit: just for completeness, I attach the code I reproduced on my laptop with default dataset: (please take care that it has no sense an exponential fit with such a dataset, but the code runs without warning)

library(ggplot2)
data('iris')
g1 <- ggplot(data=iris, aes(x=Sepal.Length, y=Sepal.Width)) + 
      geom_point(color='green') +geom_smooth(method = 'nls',
      method.args = list(start=c(a=1, b=1)), se = FALSE, 
      formula = y~a*exp(b*x), colour='black')
g1

这篇关于R ggplot2与R²和p的指数回归的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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