estimate two parameters per period

Hello

I would like briefly to refer the my previous topic that was closed yesterday:
https://forum.posit.co/t/estimate-a-parameter-per-period/188903/15

Please, i would like to ask a precision. If i want to estimate, not one but two (or more) parameters from the example of the last topic. How would change occur? I believe that the function reflecting the estimates ("Results") would have its dimension increase by the number of estimates.

Thanks a lot.

If you estimate multiple parameters, you can store the results for each in a separate matrix or you can make an array of three dimensions where the third dimension stores the different parameters. Here is an example of manually building such an array.

Results <- array(1:12, dim = c(2,2,3), 
               dimnames = list(Start = c("5/05","5/21"), 
                               End = c("6/01", "6/15"), 
                               Param = c("Height", "Weight", "Length")))

Results
#> , , Param = Height
#> 
#>       End
#> Start  6/01 6/15
#>   5/05    1    3
#>   5/21    2    4
#> 
#> , , Param = Weight
#> 
#>       End
#> Start  6/01 6/15
#>   5/05    5    7
#>   5/21    6    8
#> 
#> , , Param = Length
#> 
#>       End
#> Start  6/01 6/15
#>   5/05    9   11
#>   5/21   10   12

Results[,,2]
#>       End
#> Start  6/01 6/15
#>   5/05    5    7
#>   5/21    6    8

Created on 2024-07-29 with reprex v2.0.2

1 Like

Hello ! Thank you very much !

Please let me try and adapt your solution and come back asap.
All the best,

Again thank you very much. Of course it works but i did several attempts to adapt it to the context of the previous thread. It seems i did something wrong when for example, i use a function with three variables, say x1, x2, x3:

Results <- array(1:27, dim = c(3,3,3),
dimnames = list(Start = c("2021-01-05", "2021-04-05", "2021-05-05"),
End = c("2021-05-15", "2021-06-19", "2021-07-17"),
Param = c("x1", "x2", "x3")))

MSE_calc <- function(x1, x2, x3) {
Val <- x1P^2 - Px2^3.5 + 5*x3
SE <- (Q - Val)^2
MSE <- mean(SE)
return(MSE) }
for(j in seq_along(StartDate)) {
for(i in seq_along(EndDates)) {
if(StartDate[j] < EndDates[i]) {

#Do your calculation
DF_filter <- DF |> filter(t == StartDate, T == EndDates[i])
P <- DF_filter$P
Q <- DF_filter$Q
X <- nlm(MSE_calc, c(0.2,0.3,-0.5))
Results[j, i] <- X$estimate
				    }}}

Results

Here is you code in a single code block.

Results <- array(1:27, dim = c(3,3,3),
dimnames = list(Start = c("2021-01-05", "2021-04-05", "2021-05-05"),
End = c("2021-05-15", "2021-06-19", "2021-07-17"),
Param = c("x1", "x2", "x3")))

MSE_calc <- function(x1, x2, x3) {
Val <- x1*P^2 - P*x2^3.5 + 5*x3
SE <- (Q - Val)^2
MSE <- mean(SE)
return(MSE) }

for(j in seq_along(StartDate)) {
  for(i in seq_along(EndDates)) {
    if(StartDate[j] < EndDates[i]) {

      #Do your calculation
      DF_filter <- DF |> filter(t == StartDate, T == EndDates[i])
      P <- DF_filter$P
      Q <- DF_filter$Q
      X <- nlm(MSE_calc, c(0.2,0.3,-0.5))
      Results[j, i] <- X$estimate
				    }}}

Results

I see three problems with your code.

  1. I think that nlm() optimizes only the first parameter of the function. To get it to optimize three values, the first parameter of the function has to be a vector of length three. Your function should be something like
MSE_calc <- function(Xvec) {
Val <- Xvec[1]*P^2 - P*Xvec[2]^3.5 + 5*Xvec[3]
SE <- (Q - Val)^2
MSE <- mean(SE)
return(MSE) }
  1. Your code does no use the index j when you filter the data.
  2. When you assign X$estimate to Results, you treat Results as a two dimensional array, but it has three dimensions.

Other code comments:
You don't define the vectors StartDate and EndDates. I assume that is just an oversight in copying your code, but make sure they are correctly defined.
It is strange that you are filling the Results array with the numbers 1:27. I suggest you fill it with 0 or NA.

I suggest you first get nlm() to optimize MSE_calc() using a fixed StartDate and EndDate, without using any loops, then add the loops.

Hello,
Thank you very much for the detailed response. Please let me reshape the code and give feedback asap,
Thank you

Hello,

I agree with all your comment, did all suggestions. Step by step it seems okay. Many thanks, very relevant.

While i get three estimate indeed, the matrix results is not fed with the results as it always deliver only three estimate from X.

Below are my modifications:

MSE_calc <- function(Xvec) {
Val <- Xvec[1]*P^2 - Xvec[2]*P + Xvec[3]*P^0.5
SE <- (Q - Val)^2
MSE <- mean(SE)
return(MSE) }

..

for(z in 1:3) {
...
Results[j, i, z ] <- X$estimate
}}}
}

I believe that X$estimate is a vector of length three. You should verify that. If it is, you can't assign it to a single position in the array, such as Results[j,i,z]. You can either assign each element of X$estimate to one element of Results or assign X$estimate to three elements of Results.
To do them one at a time, use a loop.

for(z in 1:3) {
  Results[j, i, z ] <- X$estimate[z]
}

To assign three elements at once, leave the last index of Results blank.

Results[j,i, ] <- X$estimate

Here is an example of the second method.

Results <- array(NA, dim = c(3,3,3),
                 dimnames = list(Start = c("2021-01-05", "2021-04-05", "2021-05-05"),
                                 End = c("2021-05-15", "2021-06-19", "2021-07-17"),
                                 Param = c("x1", "x2", "x3")))
Results
#> , , Param = x1
#> 
#>             End
#> Start        2021-05-15 2021-06-19 2021-07-17
#>   2021-01-05         NA         NA         NA
#>   2021-04-05         NA         NA         NA
#>   2021-05-05         NA         NA         NA
#> 
#> , , Param = x2
#> 
#>             End
#> Start        2021-05-15 2021-06-19 2021-07-17
#>   2021-01-05         NA         NA         NA
#>   2021-04-05         NA         NA         NA
#>   2021-05-05         NA         NA         NA
#> 
#> , , Param = x3
#> 
#>             End
#> Start        2021-05-15 2021-06-19 2021-07-17
#>   2021-01-05         NA         NA         NA
#>   2021-04-05         NA         NA         NA
#>   2021-05-05         NA         NA         NA

Results[1,1, ] <- c(-31,25,197) 

Results
#> , , Param = x1
#> 
#>             End
#> Start        2021-05-15 2021-06-19 2021-07-17
#>   2021-01-05        -31         NA         NA
#>   2021-04-05         NA         NA         NA
#>   2021-05-05         NA         NA         NA
#> 
#> , , Param = x2
#> 
#>             End
#> Start        2021-05-15 2021-06-19 2021-07-17
#>   2021-01-05         25         NA         NA
#>   2021-04-05         NA         NA         NA
#>   2021-05-05         NA         NA         NA
#> 
#> , , Param = x3
#> 
#>             End
#> Start        2021-05-15 2021-06-19 2021-07-17
#>   2021-01-05        197         NA         NA
#>   2021-04-05         NA         NA         NA
#>   2021-05-05         NA         NA         NA

Created on 2024-08-01 with reprex v2.0.2

Thanks a lot for response.
Yes i think my piece of code is not clear because instead of estimating several time the parameters on the rolling data (from date t1 to t2 etc.), it yields only one estimation. Please let me apply your advice and come back tomorrow. Thank you.

Hello, Many thanks, i think your solution is nice, it seems to produce appropriate results, i will do a check with a complex function (like with integral) and come back. Many thanks again.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.