Calculate Rolling **Beta.distr** in column ~K and rolling ~Gamma.dist.

Hello,I am trying to calculate the rolling Beta.distr in column ~K and rolling ~Gamma.dist. in column ~b .
What do I have incorrect in my code?Could anyone help me with this?

in put code as follows:

read_file(sales1)
library(tidyverse)
library(psy)
library(lm.beta)
library(invgamma)

#Number of rows before to take into account
rolling = 2

sales1 <- sales::sales( ~date, ~sales,~K,~b,~s,~a,)
#Lag
sales1 = sales1 %>% mutate(lagsales = lag(sales))

#Aquire rolling alpha
sales1$alpha = c( rep(NA, rolling), map_dbl((rolling + 1):nrow(sales1), function(x){cronbach(sales1 %>% select(sales, lagsales) %>% slice((x-rolling):x))$alpha
})
)

#Aquire rolling beta
sales1$beta = c(rep(NA, rolling),
map_lgl((rolling + 1):nrow(sales1), function(x){lm.beta(sales1 %>% select(sales, lagsales) %>% slice((x-rolling):x))$lm.beta
})
)

#Aquire rolling gamma
sales1$invgamma = c(rep(NA, rolling),
map_dbl((rolling + 1):nrow(sales1), function(x){rinvgamma(sales1 %>% select(sales, lagsales) %>% slice((x-rolling):x))$rinvgamma
})
)

sales1

out put is as follows: Including errors

library(tidyverse)

library(psy)
library(lm.beta)
library(invgamma)

#Number of rows before to take into account
rolling = 2

sales1 <- sales::sales( ~date, ~sales,~K,~b,~s,~a,)
Error in loadNamespace(name) : there is no package called ‘sales’
#Lag
sales1 = sales1 %>% mutate(lagsales = lag(sales))

#Aquire rolling alpha
sales1$alpha = c( rep(NA, rolling), map_dbl((rolling + 1):nrow(sales1), function(x){cronbach(sales1 %>% select(sales, lagsales) %>% slice((x-rolling):x))$alpha

  • })
  • )

#Aquire rolling beta
sales1$beta = c(rep(NA, rolling),

  • map_lgl((rolling + 1):nrow(sales1), function(x){lm.beta(sales1 %>% select(sales, lagsales) %>% slice((x-rolling):x))$lm.beta
  • })
  • )
    Error in lm.beta(sales1 %>% select(sales, lagsales) %>% slice((x - rolling):x)) :
    object has to be of class lm
    Called from: lm.beta(sales1 %>% select(sales, lagsales) %>% slice((x - rolling):x))
    Browse[1]>

#Aquire rolling gamma
sales1$invgamma = c(rep(NA, rolling),

  • map_dbl((rolling + 1):nrow(sales1), function(x){rinvgamma(sales1 %>% select(sales, lagsales) %>% slice((x-rolling):x))$rinvgamma
  • })
  • )
    Error in rgamma(n, shape, rate) :
    argument "shape" is missing, with no default
    Called from: rgamma(n, shape, rate)
    Browse[1]>

sales1

A tibble: 341 x 8

Date sales K b s a lagsales alpha

1 03-0~ 40 NA NA NA NA NA NA
2 04-0~ 2 NA NA NA NA 40 NA
3 05-0~ 2 NA NA NA NA 2 0
4 06-0~ 2 NA NA NA NA 2 0
5 07-0~ 30 NA NA NA NA 2 0
6 08-0~ 2 NA NA NA NA 30 -2
7 01-0~ 9 NA NA NA NA 2 -4.44
8 02-0~ 5 NA NA NA NA 9 -1.48
9 03-0~ 65 NA NA NA NA 5 -0.0604
10 04-0~ 3 NA NA NA NA 65 -2.69

... with 331 more rows

Hi @ron4: Could you say a little about what your goal is? For example, what role does the beta distribution play in what you're trying to accomplish?

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

Hi David,
The Beta actually represents the trend ,and in the analyses,a comparison to the former value.
The Gamma shows the seasonal patterns,bot the values are held against the Alpha value,which is the baseline.
I follow the Multiplicative triple model .
This is why is i tried to put the Beta and Gamma in there :slight_smile:

Hope the above helps.
Cheers
Ron