How to put a complicated equation into a R formula?

前端 未结 3 828
谎友^
谎友^ 2020-12-16 19:13

We have the diameter of trees as the predictor and tree height as the dependent variable. A number of different equations exist for this kind of data and we try to model som

3条回答
  •  无人及你
    2020-12-16 19:50

    You've got a couple problems. (1) You're missing parentheses for the denominator of form2 (and R has no way to know that you want to add a constant a in the denominator, or where to put any of the parameters, really), and much more problematic: (2) your 2nd model isn't linear, so lm won't work.

    Fixing (1) is easy:

    form2 <- h ~ 1.3 + I(dbh^2) / (a + b * dbh + c * I(dbh^2))
    

    Fixing (2), though there are many ways to estimate parameters for a nonlinear model, the nls (nonlinear least squares) is a good place to start:

    m2 <- nls(form2, data = df, start = list(a = 1, b = 1, c = 1))
    

    You need to provide starting guesses for the parameters in nls. I just picked 1's, but you should use better guesses that ballpark what the parameters might be.

提交回复
热议问题