我创建了一个数据集来更好地理解方程并将其应用于预测行为。
方程为 y = 10/(1+k*⌊((x/t)^s)⌋)。
为了创建数据集并查看其是否正常工作,我执行了以下操作:
# creating the floor function f(x)
f = function(x) {
10/(1+1.5*(floor((x/2.5)^0.7)))
}
# specifying the domain of f(x)
x = seq(0, 100, length.out = 50) # x contains 50 points between 0 and 100
library(ggplot2)
# creating a data frame that contains x and f(x)
dat = data.frame(x = x, y = f(x))
p = ggplot(dat, aes(x = x, y = y)) +
geom_step() # geom_step creates a stairs plot
p
# adding points to the plot
p + geom_point()
然后,我想使用以下函数检查该数据集的回归分析:
#See the regression
# imports library
library(minpack.lm)
start_values <- c(k=1, s=0.3, t=2)
fit <- nls(dat$y ~ 10/(1+k*(floor((dat$x/t)^s))),
data = dat,
algorithm = "port",
start = start_values,
control = nls.control(maxiter = 1000))
summary(fit)
但是我收到以下错误:
nlsModel 中的错误(公式,mf,start,wts,upper,scaleOffset = scOff,:初始参数估计时的奇异梯度矩阵
我应该怎样做才能避免这种情况?或者我应该执行哪种分析?我不是统计专家。
感谢您的帮助!
这种类型的模型不适用于基于导数的算法。您可以在立方体中的 1000 个点上评估模型并选择最好的。
f <- function(x) {
10/(1+1.5*(floor((x/2.5)^0.7)))
}
x <- seq(0, 100, length.out = 50)
dat <- data.frame(x = x, y = f(x))
library(nls2)
set.seed(123)
start_values <- data.frame(k = 1:2, s = 0:1, t = c(1, 3))
fit <- nls2(y ~ 10/(1+k*(floor((x/t)^s))), data = dat,
algorithm = "random", start = start_values,
control = nls.control(maxiter = 1000))
fit
## Nonlinear regression model
## model: y ~ 10/(1 + k * (floor((x/t)^s)))
## data: dat
## k s t
## 1.6088 0.7009 2.5114
## residual sum-of-squares: 0.2312
##
## Number of iterations to convergence: 1000
## Achieved convergence tolerance: NA
plot(y ~ x, dat)
lines(fitted(fit) ~ x, dat, col = "red")