-1

I have values from field tests.

And I need to do the regression fitting in the next equation:

y = ax^n + bx

But I try with linear regression, no linear, etc and not works.

Data for the example:

+-----+---------+
|  X  |    Y    |
+-----+---------+
|  1  |      7  |
|  3  |    255  |
|  5  |   1895  |
|  8  |  12320  |
+-----+---------+
IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • It's very difficult to know what you've tried and what you mean by it not working. – camille Dec 03 '19 at 17:48
  • You should be able to find a sufficiently similar problem by searching SO and including the term `nls`. It's a base-R function that allows iterative fitting of nonlinear equations to data. If you are not then you should post your attempts and any error messages with an [edit] to your question body above. – IRTFM Dec 05 '19 at 01:40
  • I decided to close because it was a simple matter to modify the polynomial model in the answer to that question to fit this problem. Admittedly one needs to realize that with this synthetic data that a perfect fit of the data will cause an error, but as it stands the question is not being monitored and modified to meet SO standards for a "good question", so referral to a much better questions with a good answer seemed appropriate. – IRTFM Dec 06 '19 at 00:26

1 Answers1

2

First a reproducible dataset in R code using data from your question lightly edited:

dat <- read.table(text="| X | Y |   #you _should_ have offered output from dput(dat)
| 1 | 7 |
| 3 | 255 |
| 5 | 1895 |
| 8 | 12320 |", header=TRUE, sep="|")[2:3] # only the middle 2 columns are useful.

Then you need to know that the "^" operator in R is not exponentiation inside an R formula expression. You will need to a) use the I function and also +0 to omit an estimated intercept.

n=4; lm( Y ~ I(X^n)+X+0, data=dat)

Call:
lm(formula = Y ~ I(X^n) + X + 0, data = dat)

Coefficients:
I(X^n)       X  
     3       4  

If you assign the result of the lm call to my.lm, you can look at the fitted values with plot

> png()
> plot(dat$X, predict(my.lm))
> dev.off()

enter image description here

IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • thanks! But with the regression I need to obtain the a, b and n values. – Francisco Romero Miranda Dec 04 '19 at 20:46
  • The method gives you a and b and with n=4, the residuals are tiny so it seems unlikely that you will be able to improve very much on that guess. If you don't know the degree of the `X^n` term, then you will not be able to do regression, but will rather need to use an optimization strategy. You should also use SO's [edit] facilities to make you question more clear about what is the data situation and what is the needed output. – IRTFM Dec 04 '19 at 22:19