且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将复杂的方程放入R公式中?

更新时间:1970-01-01 07:59:00

假设你使用的是 nls R 公式可以使用普通的 R 函数,H(a, b, c,D),所以公式可以是 h ~ H(a, b, c, dbh) 并且这有效:

Assuming you are using nls the R formula can use an ordinary R function, H(a, b, c, D), so the formula can be just h ~ H(a, b, c, dbh) and this works:

# use lm to get startingf values
lm1 <- lm(1/(h - 1.3) ~ I(1/dbh) + I(1/dbh^2), df)
start <- rev(setNames(coef(lm1), c("c", "b", "a")))

# run nls
H <- function(a, b, c, D) 1.3 + D^2 / (a + b * D + c * D^2)
nls1 <- nls(h ~ H(a, b, c, dbh), df, start = start)

nls1 # display result

绘制输出:

plot(h ~ dbh, df)
lines(fitted(nls1) ~ dbh, df)