[英]curve fitting problems in R
我在这里只是一个快速的问题,因为我是R语言的新手。 我觉得这段代码不适合曲线拟合:多边形拟合肯定会检出,但是我不确定对数和指数拟合(拟合是否正确是错误的)。 任何帮助,将不胜感激,谢谢。 -山姆
polyFit <- function(xs,ys,degree) { #polynomial fitting a data set degree 3
fit3 <- lm(ys~poly(xs,degree,raw=TRUE))
xx <- seq(0,160, length=50)
plot(xs,ys,pch='@')
lines(xx, predict(fit3, data.frame(xs=xx)), col="green")
}
logFit <- function(xs,ys) { #graph the data set with log(x), y
logEstimate = lm(ys ~ log(xs))
plot(xs,ys,pch='@')
lines(xs,predict(logEstimate),col='green')
}
eFit <- function(xs,ys) {
logEstimate = lm(log(ys) ~ xs)
plot(xs,ys,pch='@')
lines(xs,predict(logEstimate),col='green')
}
为了澄清起见,xs = x点,ys = y点
不,像这样:
eFit <- function(xs,ys) {
expEstimate = lm(log(ys) ~ xs)
plot(xs, ys,pch='@')
lines(xs, exp(predict(expEstimate)),col='green')
}
我更改了模型名称,只是为了使其明显不同于您的logFit
函数,即使它在函数内部。 在该模型中,您拟合log(y)
,因此可以预测log(y)
,因此,如果要查看预测如何与实数y
叠加,则需要取消变换它们,然后exp(log(y)) = y
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.