本文介绍了R中的非线性幂回归的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个类似的问题,我想计算R中的非线性回归,但会出现错误.
I have a similar problem, I'd like to calculate the non-linear regression in R, but I get an error.
这是我的代码:
f <- function(x1,x2,x3,a,b1,b2,b3) {a * (x1^b1) * (x2^b2) * (x3^b3) }
# generate some data
x1 <- c(9,9,12,12,12,16,9,16)
x2 <- c(0.8,1,0.8,1,1.2,1.2,1.2,1)
x3 <- c(0.14,0.12,0.16,0.14,0.12,0.16,0.16,0.14)
y <- c(304,284,435,489,512,854,517,669)
dat <- data.frame(x1,x2,x3, y)
# fit a nonlinear model
fm <- nls(y ~ f(x1,x2,x3,a,b1,b2,b3), data = dat, start = c(a=0, b1=0,b2=0,b3=0))
# get estimates of a, b
co <- coef(fm)
我得到了这个错误:
我该怎么办?
谢谢!
推荐答案
您需要良好的起始值:
#starting values from linearization
fit0 <- lm(log(y) ~ log(x1) + log(x2) +log(x3), data=dat)
# fit a nonlinear model
fm <- nls(y ~ f(x1,x2,x3,a,b1,b2,b3), data = dat,
start = list(a=exp(coefficients(fit0)[1]),
b1=coefficients(fit0)[2],
b2=coefficients(fit0)[3],
b3=coefficients(fit0)[4]))
summary(fm)
# Parameters:
# Estimate Std. Error t value Pr(>|t|)
# a 265.19567 114.37494 2.319 0.081257 .
# b1 0.97277 0.08186 11.884 0.000287 ***
# b2 0.97243 0.12754 7.624 0.001589 **
# b3 0.91938 0.17032 5.398 0.005700 **
应遵循针对非线性模型推荐的常规诊断方法.
The usual diagnostics recommended for non-linear models should follow.
还请注意,起始值作为列表提供给 nls
.
Also note, that starting values are supplied to nls
as a list.
这篇关于R中的非线性幂回归的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!