1

I can't figure out how to find the best fit for an inverse equation. I recorded data and the relationship of y and x should be y=a/x were a is the value I want.

I have tried lm() but can't figure out how it works. I also tried nls, but can't figure out how that works either.

data <- read.csv("./data.csv")
y <- data[["meanacceleration"]]
x <- data[["massadded"]]
u <- data[["uncertainty"]]
exponential.model <- nls(y ~ a/x, start = (a = 1))
print(cor(y, predict(exponential.model)))
print(summary(exponential.model))
xx <- seq(240,1000, length=1000)
# massadded.exponential2 <- exp(predict(exponential.model, list(x = xx)))
plot(x, y, xlab = "Total mass", ylab="Mean Acceleration",ylim = range(c(y-u,y+u)) , pch=16)
arrows(x, y-u, x, y+u, length=0.1, angle=90, code=3)
lines(xx, predict(exponential.model), lty=2,col="red",lwd=3)


The error I got was Error in mget(names(ind), env) : invalid first argument

  • It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Oct 18 '19 at 16:24
  • x is 266.67 390.94 515.26 639.53 763.85 888.16 1012.47 and y is 0.64693 0.44720 0.34464 0.26055 0.21952 0.19185 0.15060 and in that order where x[1] equals y[1] – Charlie Ciampa Oct 18 '19 at 16:44
  • that worked. thanks – Charlie Ciampa Oct 18 '19 at 17:00

1 Answers1

1

The trouble is how the lm function is handling the 1/x term. In order for lm to recognize the inverse use the asis function I() on "1/x" like this: lm(y~I(1/x)). Now the formula with take the inverse on x prior to evalulating the linear regression.

x<-c(266.67, 390.94, 515.26, 639.53, 763.85, 888.16 ,1012.47)
y<-c(0.64693, 0.44720, 0.34464, 0.26055, 0.21952, 0.19185, 0.15060 

model <- lm(y~I(1/x))
print(cor(y, predict(model)))
print(summary(model))
     xx <- seq(240,1000, length=1000)
     prediction<-data.frame(x=xx)
     plot(x, y, xlab = "Total mass", ylab="Mean Acceleration" , pch=16)
     lines(prediction$x, predict(model, prediction), lty=2,col="red",lwd=3)

enter image description here

Dave2e
  • 22,192
  • 18
  • 42
  • 50