Compute a function with estimated variable

Hello,

I m refering to this thread: estimate a parameter per period
I have a question please.
@FJCC did a code to estimate the parameters of a function (thread 12)
and my question was about doing the reverse, ie. computing the function "Val" with the estimated parameters (x) respecting the start/end dates of the code. In clear, now we estimated the function, let's calculate the "Val" for each row of the sample while all inputs are changing each row except the "x" estimated parameters which change per periods.
Thank you.

I think what you need to do is make a data frame that has the start and end date of each period and the corresponding x value. You can then join that to the original data and calculate the Val. Here is a very simple example where Val is just P^2 + x.

library(tidyverse)
DF <- data.frame(StartDate = ymd(c("2025-04-01","2025-04-01",
                                   "2025-04-15","2025-04-15")),
                 EndDate = ymd(c("2025-04-09","2025-04-09",
                                 "2025-04-25","2025-04-25")),
                 P = c(1,2,3,4),
                 Q = c(11.2, 13, 30, 37))
DF
#>    StartDate    EndDate P    Q
#> 1 2025-04-01 2025-04-09 1 11.2
#> 2 2025-04-01 2025-04-09 2 13.0
#> 3 2025-04-15 2025-04-25 3 30.0
#> 4 2025-04-15 2025-04-25 4 37.0
xVals <- data.frame(StartDate = ymd(c("2025-04-01","2025-04-15")),
                    EndDate = ymd(c("2025-04-09","2025-04-25")),
                    x = c(10, 20))
xVals
#>    StartDate    EndDate  x
#> 1 2025-04-01 2025-04-09 10
#> 2 2025-04-15 2025-04-25 20
DFnew <- inner_join(DF, xVals, by = c("StartDate", "EndDate"))
DFnew
#>    StartDate    EndDate P    Q  x
#> 1 2025-04-01 2025-04-09 1 11.2 10
#> 2 2025-04-01 2025-04-09 2 13.0 10
#> 3 2025-04-15 2025-04-25 3 30.0 20
#> 4 2025-04-15 2025-04-25 4 37.0 20
DFnew |> mutate(Val = P^2 + x)
#>    StartDate    EndDate P    Q  x Val
#> 1 2025-04-01 2025-04-09 1 11.2 10  11
#> 2 2025-04-01 2025-04-09 2 13.0 10  14
#> 3 2025-04-15 2025-04-25 3 30.0 20  29
#> 4 2025-04-15 2025-04-25 4 37.0 20  36

Created on 2025-04-16 with reprex v2.1.1

Hello,

Thanks a lot. It looks good. I still have a constraint about x = c(10, 20) because when we estimated the x parameter (start dates in rows and end dates in column), how to convert such a matrix into the data.frame xVals because it's quick for (10,20) but when we have hundreds of point it's longer. I hope i was clear. Thanks again.

Please upload an example of your matrix using the output of dput() and explain the date values associated with the rows and columns. The example matrix only needs to have a few rows and columns.

Many thanks for your response. Please let me give you what you ask.

** FUNCTION
Let' s say the F(var1,var2) function has three parameters to estimate:
F= P^2 - var1P + var2P^0.5
Once there are estimated; you want to use the estimate to recompute F(.) based on the 2 estimated parameters using the sample. I was asking about converting the output of the estimated parameters into column to insert in the new sample DFnew to use your final code.

**OUTPUT:

Results
, , Param = var1

Start 2021-05-15 2021-06-19 2021-07-17
2021-01-05 2.500960 1.982514 -0.500000
2021-04-05 2.547257 1.974937 1.700744
2021-05-05 2.492687 -0.500000 -0.500000

, , Param = var2

Start 2021-05-15 2021-06-19 2021-07-17
2021-01-05 2.087921 1.520727 0.300000
2021-04-05 2.118189 1.508486 1.313269
2021-05-05 2.077698 0.300000 0.300000

**DATA

dput(DF)
structure(list(t = structure(c(18632, 18632, 18632, 18632, 18722,
18722, 18722, 18722, 18722, 18722, 18722, 18722, 18722, 18722,
18722, 18722, 18752, 18752, 18752), class = "Date"), T = structure(c(18762,
18762, 18825, 18825, 18762, 18762, 18762, 18762, 18797, 18797,
18825, 18825, 18825, 18825, 18825, 18825, 18762, 18762, 18762
), class = "Date"), P = c(0.307226768393632, 0.301688731220703,
0.397136088790893, 0.383877823336263, 0.236323307903033, 0.233372881698608,
0.231780329830088, 0.23386123036873, 0.387233363333833, 0.383031632908323,
0.363130308318273, 0.362333112682333, 0.339339339130839, 0.336762032999268,
0.333986731161336, 0.33217086638931, 0.306362313368632, 0.300638233383397,
0.303293118713379), Q = c(0.48332, 0.48332, 0.328632, 0.328632,
0.48332, 0.48332, 0.48332, 0.48332, 0.30832, 0.30832, 0.32372,
0.32372, 0.32372, 0.32372, 0.32372, 0.32372, 0.4802, 0.4802,
0.4802)), class = "data.frame", row.names = c(NA, -19L))

Here is an example of how to append the Var1 values to DF.

library(tidyverse)
#Manually make the matrix, since you provided only an image
Var1 <- matrix(c(2.500960, 1.982514, -0.500000,
                 2.547257, 1.974937, 1.700744,
                 2.492687, -0.500000, -0.500000), 
               nrow =3, byrow = TRUE,
               dimnames = list(c('2021-01-05', '2021-04-05','2021-05-05'), 
                               c('2021-05-15', '2021-06-19', '2021-07-17')))

#Store the start and end dates
Starts <-  dimnames(Var1)[[1]]
Ends <- dimnames(Var1)[[2]]

#Make a data frame with the matrix values
Var1_values <- as.vector(Var1)
Var1_DF <- data.frame(t = as.Date(rep(Starts, 3)),
                      T = as.Date(rep(Ends, each = 3)),
                      Var1 = Var1_values)
Var1_DF
#>            t          T      Var1
#> 1 2021-01-05 2021-05-15  2.500960
#> 2 2021-04-05 2021-05-15  2.547257
#> 3 2021-05-05 2021-05-15  2.492687
#> 4 2021-01-05 2021-06-19  1.982514
#> 5 2021-04-05 2021-06-19  1.974937
#> 6 2021-05-05 2021-06-19 -0.500000
#> 7 2021-01-05 2021-07-17 -0.500000
#> 8 2021-04-05 2021-07-17  1.700744
#> 9 2021-05-05 2021-07-17 -0.500000

#Make DF
DF <- structure(list(t = structure(c(18632, 18632, 18632, 18632, 18722,
                                     18722, 18722, 18722, 18722, 18722, 18722, 18722, 18722, 18722,
                                     18722, 18722, 18752, 18752, 18752), class = "Date"), 
                     T = structure(c(18762,18762, 18825, 18825, 18762, 18762, 18762, 18762, 18797, 18797,
                                     18825, 18825, 18825, 18825, 18825, 18825, 18762, 18762, 18762), class = "Date"), 
                     P = c(0.307226768393632, 0.301688731220703,
                           0.397136088790893, 0.383877823336263, 0.236323307903033, 0.233372881698608,
                           0.231780329830088, 0.23386123036873, 0.387233363333833, 0.383031632908323,
                           0.363130308318273, 0.362333112682333, 0.339339339130839, 0.336762032999268,
                           0.333986731161336, 0.33217086638931, 0.306362313368632, 0.300638233383397,
                           0.303293118713379), 
                     Q = c(0.48332, 0.48332, 0.328632, 0.328632,0.48332, 0.48332, 0.48332, 0.48332, 0.30832, 0.30832, 0.32372,
                           0.32372, 0.32372, 0.32372, 0.32372, 0.32372, 0.4802, 0.4802,
                           0.4802)), class = "data.frame", row.names = c(NA, -19L))

#Join the Var1 values to DF
DF <- inner_join(DF, Var1_DF, by = c("T","t"))

Created on 2025-04-18 with reprex v2.1.1

Thanks a lot. Please let me try and come back.
Just to respond to "Manually make the matrix, since you provided only an image", i just copy paste the results as it was for a basic example but i can add the code. Again thank you,and come back asap.

Thank you again for your code. I now understand your point on the image.Please
let me provide you the dput of the two variables so that you could explain me how to append both variables without manually report them to avoid mistakes.

dput(Results)
structure(c(2.50096020091677, 2.54725668147744, 2.49268651598907,
1.98251404359226, 1.97493725869699, -0.5, -0.5, 1.70074362934695,
-0.5, 2.08792096570695, 2.11818891732871, 2.07769801039209, 1.52072714171543,
1.50848580270179, 0.3, 0.3, 1.31326906963846, 0.3), dim = c(3L,
3L, 2L), dimnames = list(Start = c("2021-01-05", "2021-04-05",
"2021-05-05"), c("2021-05-15", "2021-06-19", "2021-07-17"), Param = c("var1",
"var2")))

You can replace my manual definition of Var1 with extracting the "var1" subset of your Results array. The rest of the code would be unchanged.

Results <- structure(c(2.50096020091677, 2.54725668147744, 2.49268651598907,
            1.98251404359226, 1.97493725869699, -0.5, -0.5, 1.70074362934695,
            -0.5, 2.08792096570695, 2.11818891732871, 2.07769801039209, 1.52072714171543,
            1.50848580270179, 0.3, 0.3, 1.31326906963846, 0.3), 
          dim = c(3L,3L, 2L), 
          dimnames = list(Start = c("2021-01-05", "2021-04-05","2021-05-05"), 
                          c("2021-05-15", "2021-06-19", "2021-07-17"), 
                          Param = c("var1","var2")))
Var1 <- Results[, ,"var1"]
Var1
#>             
#> Start        2021-05-15 2021-06-19 2021-07-17
#>   2021-01-05   2.500960   1.982514  -0.500000
#>   2021-04-05   2.547257   1.974937   1.700744
#>   2021-05-05   2.492687  -0.500000  -0.500000

Created on 2025-04-19 with reprex v2.1.1

Thank you. So i extract Var1 then do the same for Var2 and run the rest of the code. Please let me try and come back asap. Many thanks.

Hello,
Many thanks !
Please i have a precision to ask. I wonder how would you modify the "each"
in "as.Date(rep(Starts, each = 4)" if you have unbalanced data where some products have
in day1 for example differents expiration date? for example, product1 in day 1 has 3 expiration
dates, product2 in day1 have 5 etc (although the Start/End remain identical).
Thanks a lot.

Sorry, I don't understand your explanation. I don't know how "products" and "expiration date" relate to the data you have posted. Please explain that and post new data if necessary.

I'm very sorry i was not clear at all. I try to say it clearly otherwise i look for new sample with unbalanced data. Let's skip the 'product' mentionning. 'Expiration' corresponds to "End". I meant that in your code we assume (because it is like this in the sample) that there are 3 starts/end dates. My questions was to know in case there are not 3 but other number that i want the code to count.
Thank you again.

The values used in the rep() function depend on the number of start dates and end dates. The Starts vector is repeated as many times as there are End dates and the End date values are each repeated as many times as there are Start dates. The logic follows from the way the Results matrix is is turned into a vector. The vector values come from scanning down each column of the Results matrix, so the Start date values go through their sequence and the End date remains constant while moving down a column.
Here is how I would set up the code.

Results <- structure(c(2.50096020091677, 2.54725668147744, 2.49268651598907,
            1.98251404359226, 1.97493725869699, -0.5, -0.5, 1.70074362934695,
            -0.5, 2.08792096570695, 2.11818891732871, 2.07769801039209, 1.52072714171543,
            1.50848580270179, 0.3, 0.3, 1.31326906963846, 0.3), 
          dim = c(3L,3L, 2L), 
          dimnames = list(Start = c("2021-01-05", "2021-04-05","2021-05-05"), 
                          c("2021-05-15", "2021-06-19", "2021-07-17"), 
                          Param = c("var1","var2")))
Var1 <- Results[, ,"var1"]
Var1
#Store the start and end dates
Starts <-  dimnames(Var1)[[1]]
Count_Starts <- length(Starts)
Ends <- dimnames(Var1)[[2]]
Count_Ends <- length(Ends)
#Make a data frame with the matrix values
Var1_values <- as.vector(Var1)
Var1_DF <- data.frame(t = as.Date(rep(Starts, Count_Ends)),
                      T = as.Date(rep(Ends, each = Count_Starts)),
                      Var1 = Var1_values)