89_Simple
89_Simple

Reputation: 3805

Running a function for each group

I have following parameters of gompertz

  A <- 100 # A is always 100
  mu <- 35
  lambda <- 265 # day of the year. Also the start day

I can use the above parameters to run a gompertz using following equation

   grofit::gompertz(time,A,mu,lambda)

time is a basically a vector of lambda:end.day.

Now the issue is that I know the lambda (start day) but not the end day. I want find the end day when it reaches 100.

For e.g in the above example if I supply lambda:end.day as 265:270, I do not reach 100.

   time <- 265:270  
   x <- round(grofit::gompertz(time,A,mu,lambda),2)
   x
   6.60 35.00 66.67 85.51 94.13 97.69

By multiple trials, I know if I give a vector of 265:277, I will reach 100.

   time <- 265:277
   x <- round(grofit::gompertz(time,A,mu,lambda),2)
   x

  [1]  6.60  35.00  66.67  85.51  94.13  97.69
  [7]  99.10  99.65  99.87  99.95  99.98  99.99
 [13] 100.00

I have dataframe that has the lambda (same as start day) and mu.

   df <- data.frame(id = c(1,1,2,2), year = c(1981,1982,1981,1982), mu= c(35,32,33,28), lambda = c(275,278,284,296))

For each id and year, I want two columns: one column called day first value of which is equal to lamba and a second column which tells me the value of x for each day till it reaches 100 (end day).

How do I implement the above equation for each id and year such that I have a dataframe something like this:

    id  year    day x
    1     1981  275 6.6
    1     1981  276 35
    1     1981  277 66.67
    1     1981  278 85.51
    1     1981  279 94.13
    1     1981  280 97.69
    1     1981  281 99.1
    1     1981  282 99.65
    1     1981  283 99.87
    1     1981  284 99.95
    1     1981  285 99.98
    1     1981  286 99.99
    1     1981  287 100
    .     .     .     .
    .     .     .     .
    2     1982  296 8
    2     1982  297 33
    2     1982  298 45
    2     1982  299 63
    2     1982  300 61
    2     1982  301 73
    2     1982  302 81
    2     1982  303 91
    2     1982  304 94
    2     1982  305 98
    2     1982  306 99
    2     1982  307 100

Upvotes: 0

Views: 36

Answers (1)

Jack Brookes
Jack Brookes

Reputation: 3830

Using dplyr and tidyr:

library(dplyr)
library(tidyr)

A <- 100 # A is always 100

df <-
  data.frame(
    id = c(1, 1, 2, 2),
    year = c(1981, 1982, 1981, 1982),
    mu = c(35, 32, 33, 28),
    lambda = c(275, 278, 284, 296)
  )


df2 <- df %>% 
  crossing(day = 1:365) %>% 
  group_by(id, year) %>% 
  filter(day >= lambda) %>% 
  mutate(x = round(grofit::gompertz(day, A, mu, lambda), 2)) %>%
  group_by(id, year, x) %>% 
  filter(x != 100 | row_number() == 1)

df2 %>% 
  as.data.frame()

Result:

   id year mu lambda day      x
1   1 1981 35    275 275   6.60
2   1 1981 35    275 276  35.00
3   1 1981 35    275 277  66.67
4   1 1981 35    275 278  85.51
5   1 1981 35    275 279  94.13
6   1 1981 35    275 280  97.69
7   1 1981 35    275 281  99.10
8   1 1981 35    275 282  99.65
9   1 1981 35    275 283  99.87
10  1 1981 35    275 284  99.95
11  1 1981 35    275 285  99.98
12  1 1981 35    275 286  99.99
13  1 1981 35    275 287 100.00
14  1 1982 32    278 278   6.60
15  1 1982 32    278 279  32.01
16  1 1982 32    278 280  62.05
17  1 1982 32    278 281  81.87
18  1 1982 32    278 282  91.96
19  1 1982 32    278 283  96.55
20  1 1982 32    278 284  98.54
21  1 1982 32    278 285  99.39
22  1 1982 32    278 286  99.74
23  1 1982 32    278 287  99.89
24  1 1982 32    278 288  99.95
25  1 1982 32    278 289  99.98
26  1 1982 32    278 290  99.99
27  1 1982 32    278 291 100.00
28  2 1981 33    284 284   6.60
29  2 1981 33    284 285  33.01
30  2 1981 33    284 286  63.64
31  2 1981 33    284 287  83.17
32  2 1981 33    284 288  92.76
33  2 1981 33    284 289  96.98
34  2 1981 33    284 290  98.76
35  2 1981 33    284 291  99.49
36  2 1981 33    284 292  99.79
37  2 1981 33    284 293  99.92
38  2 1981 33    284 294  99.97
39  2 1981 33    284 295  99.99
40  2 1981 33    284 296  99.99
41  2 1981 33    284 297 100.00
42  2 1982 28    296 296   6.60
43  2 1982 28    296 297  28.09
44  2 1982 28    296 298  55.26
45  2 1982 28    296 299  75.80
46  2 1982 28    296 300  87.86
47  2 1982 28    296 301  94.13
48  2 1982 28    296 302  97.21
49  2 1982 28    296 303  98.69
50  2 1982 28    296 304  99.39
51  2 1982 28    296 305  99.71
52  2 1982 28    296 306  99.87
53  2 1982 28    296 307  99.94
54  2 1982 28    296 308  99.97
55  2 1982 28    296 309  99.99
56  2 1982 28    296 310  99.99
57  2 1982 28    296 311 100.00

Upvotes: 1

Related Questions