[英]Find equation of a line to fit data in R
I have just started learning R and trying to show the relationship between the mpg of a car and the horsepower.我刚刚开始学习 R 并试图展示汽车的 mpg 和马力之间的关系。 I was trying to get a line of best fit to match the data so I can use it to predict other values.我试图获得一条最适合的线来匹配数据,这样我就可以用它来预测其他值。
#read data from csv file
data <- read.csv("auto-mpg.csv")
#get data
df <- head(data, 350)
#set x and y
x <- df$horsepower
y <- df$mpg
#change x from string to int
x <- strtoi(x)
#create scatterplot
scatter.smooth(x, y, main = "mpg vs horsepower",
xlab = "horsepower", ylab = "mpg",
pch = 19)
This is the graph that I want the exponential decay for这是我想要指数衰减的图表
Here are two approaches.这里有两种方法。 First by using linear fit to log(y) as suggested by @Gregor Thomas and then a nonlinear fit to the original data.首先,按照@Gregor Thomas 的建议对 log(y) 进行线性拟合,然后对原始数据进行非线性拟合。 You did not provide reproducible data so I'm using similar data from mtcars
:您没有提供可重现的数据,所以我使用来自mtcars
的类似数据:
data(mtcars)
plot(mpg~hp, mtcars, pch=20)
mpg.lm <- lm(log(mpg)~hp, mtcars) # Model is log(mpg) = a + b * hp
mpg.lm
#
# Call:
# lm(formula = log(mpg) ~ hp, data = mtcars)
#
# Coefficients:
# (Intercept) hp
# 3.460467 -0.003429
#
X <- seq(50, 350)
Y1 <- exp(predict(mpg.lm, data.frame(hp=X)))
lines(X, Y1, col="red")
The red line shows the fit.红线表示合身。 Now using nls
to fit a nonlinear model directly.现在使用nls
直接拟合非线性 model。 We'll use the coefficients from the first model to estimate the starting values for the second:我们将使用第一个 model 的系数来估计第二个的起始值:
a1 <- coef(mpg.lm)[1]
b1 <- coef(mpg.lm)[2]
mpg.nls <- nls(mpg ~ a * hp^b, mtcars, start=list(a=exp(a1), b=b1))
mpg.nls # Model is mpg = a * hp^b
# Nonlinear regression model
# model: mpg ~ a * hp^b
# data: mtcars
# a b
# 272.1171 -0.5404
# residual sum-of-squares: 288.7
#
# Number of iterations to convergence: 7
# Achieved convergence tolerance: 1.127e-07
Y2 <- predict(mpg.nls, data.frame(hp=X))
lines(X, Y2, col="blue")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.