简体   繁体   English

在 R 中执行幂回归

[英]Perform Power Regression in R

I am having problems fitting a potential regression model of type Y = aX^b .我在拟合Y = aX^b类型的潜在回归模型时遇到问题。 Here is some context.这是一些上下文。

I have the following data vectors:我有以下数据向量:

x <- c(2.08, 1.99, 2.03, 2.01, 2.10, 1.91, 1.84, 2.16, 2.04, 2.05, 2.04, 1.97, 2.03, 2.11, 2.06, 2.07, 2.12, 1.98, 2.13, 2.13, 1.97, 1.79, 2.11, 2.09, 2.19, 2.07, 1.99, 2.03, 2.12, 2.14)*100
y <- c(157.91, 138.47, 146.26, 142.81, 161.77, 123.76, 109.68, 175.48, 149.84, 151.99, 149.39, 134.55, 147.54, 164.49, 153.63, 154.44, 167.12, 136.43, 169.25, 168.22, 134.32, 101.56, 164.96, 160.17, 182.02, 154.95, 137.78, 147.75, 166.54, 171.11)

plot(x,y)

Although this toy data fits a linear model well (R^2 0.997), actually my data has a wider range of X ranging from 5 to 450 and I intuit that it is a better fit to a function of the type Y = aX^b .虽然这个玩具数据很适合线性模型 (R^2 0.997),但实际上我的数据具有更宽的 X 范围,从 5 到 450,我直觉它更适合Y = aX^b类型的函数.

I am trying to fit a model linearizing X and Y using log(x) and log(y) .我正在尝试使用log(x)log(y)来拟合线性化 X 和 Y 的模型。

fit <- lm(log(y)~log(x))
plot(x,y)
lines(x, exp(fit$fitted.values), col="red")

在此处输入图像描述

However, the plot does not make sense since many lines appear.然而,情节没有意义,因为出现了很多台词。 How can I improve this graph?我怎样才能改进这个图表? Am I fitting the model incorrectly or am I plotting wrong?我是在错误地拟合模型还是在绘制错误?

If I print the following I can get the summary of the model:如果我打印以下内容,我可以获得模型的摘要:

summary(fit)

Output:输出:

> summary(fit)

Call:
lm(formula = log(y) ~ log(x))

Residuals:
       Min         1Q     Median         3Q        Max 
-0.0064932 -0.0026293 -0.0003367  0.0026992  0.0065128 

Coefficients:
             Estimate Std. Error t value Pr(>|t|)    
(Intercept) -10.49654    0.08586  -122.3   <2e-16 ***
log(x)        2.91462    0.01614   180.6   <2e-16 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 0.003912 on 28 degrees of freedom
Multiple R-squared:  0.9991,    Adjusted R-squared:  0.9991 
F-statistic: 3.261e+04 on 1 and 28 DF,  p-value: < 2.2e-16

How can I obtain the RMSE?如何获得 RMSE? And how can I get the equation that defines the model?我怎样才能得到定义模型的方程式? That is to say, that they are worth a and b in the equation Y = aX^b .也就是说,它们在等式Y = aX^b中值ab

The issue is in the plot rather than the model, if plotting lines your data should be ordered, otherwise you will get a very zig-zaggy thing, as you noticed.问题出在绘图而不是模型中,如果绘制线条,您的数据应该被排序,否则您会得到一个非常曲折的东西,正如您注意到的那样。 Try尝试

lines(sort(x), exp(fit$fitted.values)[order(x)], col="red")

or alternatively sort your data before running the model.或者在运行模型之前对数据进行排序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM