Fitting a function in R

后端 未结 3 1170
抹茶落季
抹茶落季 2021-02-04 09:25

I have a few datapoints (x and y) that seem to have a logarithmic relationship.

> mydata
    x   y
1   0 123
2   2 116
3   4 113
4  15 100
5  48  87
6  75  84         


        
3条回答
  •  温柔的废话
    2021-02-04 09:51

    Try taking the log of your response variable and then using lm to fit a linear model:

    fit <- lm(log(y) ~ x, data=mydata)
    

    The adjusted R-squared is 0.8486, which at face value isn't bad. You can look at the fit using plot, for example:

    plot(fit, which=2)
    

    But perhaps, it's not such a good fit after all:

    last_plot() + geom_line(aes(x=x, y=exp(fit$fitted.values)))
    

提交回复
热议问题