简体   繁体   中英

How to find the best fit of an inverse equation in R

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

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)

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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