3

I'm trying to run a nonlinear regression of the form y_i = sum_t(x_{it}^b)

on the following (simplified) dataset:

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))

Which gives:

       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

That is, there are 16 uniques values of x but only 4 unique observations of y, and each y is determined by the summation of x^2 that share the same t. I want to run an nls regression along the lines of:

fit <- nls(y ~ sum(x^b), data = df, start=list(b = 2))

With the hope that b will be 2, but I don't know how to write the equation for the fit such that x is summed by groups (of t), instead of being summed altogether.

Thank you.

  • 1
    Can you elaborate on what you mean by "each 'y' is determined by the summation of 'x^2' that share the same 't' " please? Could you also use this example to explicitly display your design matrix (after performing your desired calculations) and your corresponding vector of responses please? – NM_ Mar 17 '19 at 04:57
  • @Nishan For instance, when t = 1, y = 13^2 + 11^2 + 5^2 + 9^2 = 396, and so on for other t. I want to be able the run the nls so that the result will be b = 2, but at the moment, when I run `fit <- nls(y ~ sum(x^b), data = df, start=list(b = 2))`, I get `Nonlinear regression model model: y ~ sum(x^b) data: df b 1.43 residual sum-of-squares: 529112 Number of iterations to convergence: 5 Achieved convergence tolerance: 1.11e-08` – nicholasflamel Mar 17 '19 at 05:02

1 Answers1

2

I see two things:

  1. For grouping I will use nlsList which it is much more convenient for your model (Fitting nls to grouped data R)
  2. Your model is too much perfect, your "forgot" the error! Because much of the math require to inverse matrices and all this stuff, with your "perfect" model there are issues. Just add a little error! (As we say in Spain: Perfection is fascist!)

    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)
    

Results

1 2.000428
2 2.000314
3 2.000486
4 2.002057
LocoGris
  • 4,432
  • 3
  • 15
  • 30