R中的功效回归与Excel相似 [英] Power regression in R similar to excel

查看:76
本文介绍了R中的功效回归与Excel相似的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的数据集,我正在尝试使用幂趋势来最佳拟合数据.示例数据非常小,如下所示:

structure(list(Discharge = c(250, 300, 500, 700, 900), Downstream = c(0.3, 
0.3, 0.3, 0.3, 0.3), Age = c(1.32026239202165, 1.08595138888889, 
0.638899189814815, 0.455364583333333, 0.355935185185185)), .Names = c("Discharge", 
"Downstream", "Age"), row.names = c(NA, 5L), class = "data.frame")

数据如下:

> new
  Discharge Downstream       Age
1       250        0.3 1.3202624
2       300        0.3 1.0859514
3       500        0.3 0.6388992
4       700        0.3 0.4553646
5       900        0.3 0.3559352

我尝试使用ggplot2

绘制以上数据

ggplot(new)+geom_point(aes(x=Discharge,y=Age))

我可以使用geom_smooth(method="lm")添加线性线,但不确定要显示电源线需要什么代码.

输出如下:

如何像在excel中一样添加幂线性回归线? excel数字如下所示:

解决方案

使用nls(非线性最小二乘法)作为平滑器

例如

ggplot(DD,aes(x = Discharge,y = Age)) +
  geom_point() + 
  stat_smooth(method = 'nls', formula = 'y~a*x^b', start = list(a = 1,b=1),se=FALSE)

注意到Doug Bates对R平方值和非线性模型的评论在图形上添加回归线方程和R2

追加回归线方程

# note that you have to give it sensible starting values
# and I haven't worked out why the values passed to geom_smooth work!
power_eqn = function(df, start = list(a =300,b=1)){
  m = nls(Discharge ~ a*Age^b, start = start, data = df);
  eq <- substitute(italic(y) == a  ~italic(x)^b, 
               list(a = format(coef(m)[1], digits = 2), 
                    b = format(coef(m)[2], digits = 2)))
  as.character(as.expression(eq));                 
}

ggplot(DD,aes(x = Discharge,y = Age)) +
  geom_point() + 
  stat_smooth(method = 'nls', formula = 'y~a*x^b', start = list(a = 1,b=1),se=FALSE) +  
  geom_text(x = 600, y = 1, label = power_eqn(DD), parse = TRUE)

I have a simple dataset and I am trying to use the power trend to best fit the data. The sample data is very small and is as follows:

structure(list(Discharge = c(250, 300, 500, 700, 900), Downstream = c(0.3, 
0.3, 0.3, 0.3, 0.3), Age = c(1.32026239202165, 1.08595138888889, 
0.638899189814815, 0.455364583333333, 0.355935185185185)), .Names = c("Discharge", 
"Downstream", "Age"), row.names = c(NA, 5L), class = "data.frame")

Data looks as follows:

> new
  Discharge Downstream       Age
1       250        0.3 1.3202624
2       300        0.3 1.0859514
3       500        0.3 0.6388992
4       700        0.3 0.4553646
5       900        0.3 0.3559352

I tried to plot the above data using ggplot2

ggplot(new)+geom_point(aes(x=Discharge,y=Age))

I could add the linear line using geom_smooth(method="lm") but I am not sure what code do I need to show the power line.

The output is as follows:

How Can I add a power linear regression line as done in excel ? The excel figure is shown below:

解决方案

Use nls (nonlinear least squares) as your smoother

eg

ggplot(DD,aes(x = Discharge,y = Age)) +
  geom_point() + 
  stat_smooth(method = 'nls', formula = 'y~a*x^b', start = list(a = 1,b=1),se=FALSE)

Noting Doug Bates comments on R-squared values and non-linear models here, you could use the ideas in Adding Regression Line Equation and R2 on graph

to append the regression line equation

# note that you have to give it sensible starting values
# and I haven't worked out why the values passed to geom_smooth work!
power_eqn = function(df, start = list(a =300,b=1)){
  m = nls(Discharge ~ a*Age^b, start = start, data = df);
  eq <- substitute(italic(y) == a  ~italic(x)^b, 
               list(a = format(coef(m)[1], digits = 2), 
                    b = format(coef(m)[2], digits = 2)))
  as.character(as.expression(eq));                 
}

ggplot(DD,aes(x = Discharge,y = Age)) +
  geom_point() + 
  stat_smooth(method = 'nls', formula = 'y~a*x^b', start = list(a = 1,b=1),se=FALSE) +  
  geom_text(x = 600, y = 1, label = power_eqn(DD), parse = TRUE)

这篇关于R中的功效回归与Excel相似的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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