2014-10-30 57 views
0

因为我是R语言的新手,所以在这里我只提一个简短的问题。我不觉得这个代码适合曲线拟合:poly拟合肯定会检查出来,但是我不确定的日志和指数拟合(e fit是否错误)。任何帮助将不胜感激,谢谢。萨姆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点

+2

我认为你的模型很好。可能你的问题是你没有改变你的预测。例如,在你的'eFit'中,你预测'log(ys)',并将它们绘制成'ys'。可能你应该画出'exp(预测(logEstimate))'。 – Gregor 2014-10-30 18:18:01

+0

所以像这样?:eFit < - function(xs,ys)logEstimate = lm(log(ys)〜xs) plot(xs,ys,pch ='@') lines(predict(logEstimate),ys ,col ='green') } – user3558177 2014-10-30 18:46:15

回答

0

没有,像这样:

eFit <- function(xs,ys) { 
    expEstimate = lm(log(ys) ~ xs) 
    plot(xs, ys,pch='@') 
    lines(xs, exp(predict(expEstimate)),col='green') 
} 

我改变了型号名称只是为了清楚地区别于你的即使它是函数的内部函数,也是如此。在这个模型中,你适合log(y),所以你预测log(y),所以如果你想看看你的预测如何与真实的y叠加起来,你需要对它们进行非转换,并且exp(log(y)) = y

+0

ahhhh好的我看到感谢的人。 – user3558177 2014-10-30 19:57:58