我正在尝试运行y_i = sum_t(x_ {it} ^ b)形式的非线性回归
在以下(简化)数据集上:
require(dplyr)
set.seed(2019)
df <- data.frame(t = rep(1:4, each = 4),
x = sample(1:16, replace=F))
df <- df %>%
group_by(t) %>%
mutate(y = sum(x^2))
这使:
t x y
<int> <int> <dbl>
1 1 13 396
2 1 11 396
3 1 5 396
4 1 9 396
5 2 1 626
6 2 12 626
7 2 16 626
8 2 15 626
9 3 10 361
10 3 14 361
11 3 7 361
12 3 4 361
13 4 8 113
14 4 6 113
15 4 2 113
16 4 3 113
也就是说,x
有16个唯一值,但只有4个独特的y
观测值,每个y
由共享相同x^2
的t
的总和决定。我想按照以下方式运行nls
回归:
fit <- nls(y ~ sum(x^b), data = df, start=list(b = 2))
希望b
将是2,但我不知道如何编写适合的等式,使x
由(t
)群组合,而不是完全求和。
谢谢。
我看到两件事:
nlsList
,它更方便您的模型(Fitting nls to grouped data R)library(nlsList)
set.seed(2019)
df <- data.frame(t = rep(1:4, each = 4),
x = sample(1:16, replace=F))
df <- df %>%
group_by(t) %>%
mutate(y = sum(x^2)+runif(4,0,1))
rec.hyp <- nlsList(y ~ sum(x^b) | t,
data=df,
start=c(b=2),
na.action=na.omit)
coef(rec.hyp)
结果
1 2.000428
2 2.000314
3 2.000486
4 2.002057