打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
basicTrendline:CRAN最新发布的线性非线性拟合的R函数包介绍

CRAN官网链接: https://cran.r-project.org/web/packages/basicTrendline/index.html

使用过nls()来作非线性回归的朋友,感觉都要吐槽一下nls(formula, start = list(a = , b = )里面的起始值(a,b)的设定,n次都设不对是怎样一个感受?抓狂到想要放弃用R有木有?

其实我们只想要像excel里面添加趋势线那样简单,就能得到非线性回归方程的各个结果而已。

最新CRAN正式上线的R函数包——“basicTrendline” package 终于解决了这个问题。

先来看两张效果图(第一张为log线性回归,第二张为包含线性和非线性回归方程的组合图):



闲言太多,步入正题!

R函数包”basicTrendline”到底用来干什么?

用于一步完成绘图,添加线性或非线性拟合线,在图上显示回归方程及R2和回归模型的p值(不是参数的p值)。 并且,它默认会同时输出模型summary()的结果,即各参数的具体数值及SD值,t值,p值等等。

一个严肃的问题:它结果可信吗?

  • 我们已经检测了我们的R函数包“basicTrendline”, 它工作性能稳定;

  • 更重要的是它的拟合结果和商业软件OriginPro完全相等

  • 甚至对幂函数power函数(y=a*x^b +c)比OriginPro软件更好
    (更高的R2,更低的p值,因为我们采取了优于Origin软件的selfStart计算方法)!

在R中安装 “basicTrendline” 函数包

1. 直接从CRAN官方获取资源安装:

install.packages('basicTrendline')library(basicTrendline)

2. 使用Github资源安装:

install.packages('devtools')library(devtools)   install_github('PhDMeiwp/basicTrendline@master', force = TRUE)library(basicTrendline)

使用 “basicTrendline” 函数包

建立 x,y数据集,比如:

x<><>

然后运行:

library(basicTrendline)trendline(x,y,model='exp3P', summary=TRUE, paramDigit=10, legendPos='topleft',linecolor='red')  

你能用通用的函数 “trendline()”,但只需改变参数 model 的值,即可输出不同的回归模型的结果以及图。

参数“model” 的值为 ‘lin2P’,’line3P’,’log2P’,’exp3P’,’power3P’的其中一个。

  • “line2P”    # y=a*x+b

  • “line3P”    # y=a*x^2+b*x+c

  • “log2P”     # y=a*ln(x)+b

  • “exp3P”     # y=a*exp(b*x)+c

  • “power3P”   # y=a*x^b+c)

创新点(再强调一下而已)

  • “basicTrendline”函数包对幂函数的拟合(’power3P’ model:y = ax^b +c)能得到比OriginPro软件更好的结果(更高的R^2,更低的p值)。为什么呢?因为我们采取了比OriginPro软件更高级的针对幂函数回归的selfStart算法呀,其实核心是OriginPro软件里面的幂函数回归没有考虑数据的 凹凸趋势(增凹趋势,降凸趋势等等,文末 Examples 的代码里有提到)。下面是例子:

formula as y=a*x^b +c

x<->

y<- c(2,14,16,18,19,20)=""  #="" increase="">

OriginPro软件结果:

c=-7344.578

a=7347.183

b=43224.4

adjR^2= 0.97129       # lower adjR^2 value

p-value of model=2.24891e-4

‘basicTrendline’函数包结果:

c=34.671

a=-32.703

b=-0.13999

adjR^2= 0.99346 >  0.97129   # higher (or better ) adjR^2 value

p-value of model= 2.44924-5 <>

输出图








查看更多”basicTrendline”函数包的功能或使用方法, 请运行:

library(basicTrendline)?trendline()

上面各种类型的回归以及最后的组合图的例子,代码在 ?trendline()的 Examples 里面都有哦。下面也copy一下方便直接查看。

Examples (R code)

#先设计一些数据x1<><-><-><-><-><-c(2,14,18,19,20)  =""  =""  =""  #="" 增凸趋势="" increasing="" convex=""><- c(-2,-14,-18,-19,-20)=""  #="" 降凹趋势="" decreasing="" concave=""><-c(2,4,16,38,89)  =""  =""  =""  ="" #="" 增凹趋势="" increasing="" concave=""><-c(-2,-4,-16,-38,-89)  =""  #="" 降凸趋势="" decreasing="" convex=""><- c(600002,600014,600018,600019,600020)="" #="" high="" y="" values="" with="" low="">

上面都是输入数据,下面才是核心,代码运行很简单有木有?

library(basicTrendline)trendline(x1,y1,model='line2P',summary=TRUE,eDigit=10) #第一张图的代码已完成。trendline(x2,y2,model='line3P',summary=FALSE,ePos='topright') #第二张图的代码已完成,诸如此类。trendline(x3,y3,model='log2P',linecolor='blue')trendline(x4,y4,model='exp3P',eSize=0.7) #change the font size of equation.trendline(x5,y5,model='power3P')## Not run 绘制组合图plot(x1,y1,main='Different regression lines in one plot') #绘制底图散点图library(basicTrendline)trendline(x1,y1,model='line2P',plot=FALSE,ePos='none',linecolor='red') #添加 y=a*x+b 线性回归trendline(x1,y1,model='log2P',plot=FALSE,ePos='none',linecolor='blue',lty=2) #添加 y=a*ln(x)+b 线性回归trendline(x1,y1,model='exp3P',plot=FALSE,ePos='none',linecolor='black',lty=3) #添加 y=a*exp(b*x)+c 非线性回归legend('bottomright',c('line2P','log2P','exp3P'), lty=c(1,2,3),col=c('red','blue','black')) #最后添加legend,完成。## END (Not run)

贡献者

R codes contributed by

  • adding-regression-line-equation-and-r2-on-graph-1: http://blog.sciencenet.cn/blog-267448-1021594.html

  • adding-regression-line-equation-and-r2-on-graph-2: https://stackoverflow.com/questions/7549694/adding-regression-line-equation-and-r2-on-graph

  • What is non-linear regression?: https://datascienceplus.com/first-steps-with-non-linear-regression-in-r/

  • adding regression line for nonlinear regression: http://blog.sciencenet.cn/blog-651374-1014133.html

  • R codes for ‘print.summary.nls of exp3P and power3P’ cite from https://github.com/SurajGupta/r-source/blob/master/src/library/stats/R/nls.R

  • and so on…

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
函数与线性回归分析
Photoshop色阶与曲线的比较
伽马空间与线性空间
为什么说Relu是非线性激活函数,在大于0部分不是线性的吗?
为什么神经网络,必须使用非线性的激活函数
SPSS统计功能与模块对照表 -SPSS应用 -中国统计网(STA8.CN) 中国最精准、最专业的统计学技术网站
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服