R/CAGR.R

Defines functions n.years LLTM data.first data.last CAGR

Documented in CAGR data.first data.last LLTM n.years

#' @title Compute CAGR(Compound Annual Growth Rate)
#' @description Compute CAGR(Compound Annual Growth Rate)
#' @param data.1 Data of the first year
#' @param data.n Data of the last year
#' @param n Number of years
#'
#' @return CAGR and between years values
#' @export
#' @usage CAGR(data.1, data.n, n)
#' @examples c.cagr<-CAGR(100, 189, 5)
#' @references Bardhan, D., Singh, S.R.K., Raut, A.A.and Athare, T.R. (2022). Livestock in Madhya Pradesh and Chhattisgarh: An Analysis for Some Policy Implications. Agricultural Science Digest. DOI:10.18805/ag.D-5418.

CAGR <- function(data.1, data.n, n){

  r<-((data.n/data.1)^(1/n)-1)
  ### generate data ###
  df<-data.frame()

  for(i in 1:n)
  {
    output=(data.1*(1+r)^i)
    df=rbind(df, output)
  }
  colnames(df)<-'y'

  y<-as.vector(rbind(data.1,df))
  y<-as.data.frame(y)

  Output_CAGR <- list(CAGR = r*100, Values = y)
  return(Output_CAGR)
}




#' @title Computing Last Year data
#' @description Computing last year data
#'
#'
#' @param data.1 Data of the first year
#' @param r CAGR
#' @param n Number of years
#'
#' @return Last year data and between years values
#' @export
#' @usage data.last(data.1, r, n)
#' @examples d.last<-data.last(100, 13.57751, 5)
#' @references Bardhan, D., Singh, S.R.K., Raut, A.A.and Athare, T.R. (2022). Livestock in Madhya Pradesh and Chhattisgarh: An Analysis for Some Policy Implications. Agricultural Science Digest. DOI:10.18805/ag.D-5418.


data.last<-function(data.1, r, n){
  df <- data.frame()

  for (i in 1:n) {
    data.n = (data.1 * (1 + r/100)^n)
    output = (data.1 * (1 + r/100)^i)
    df = rbind(df, output)
  }
  colnames(df) <- "y"
  y <- as.vector(rbind(data.1, df))
  y <- as.data.frame(y)
  Output_CAGR <- list(data.n, Values = y)
  return(Output_CAGR)
}




#' @title Computing First Year data
#' @description Computing first year data
#'
#'
#' @param data.n Data of the last year
#' @param r CAGR
#' @param n Number of years
#'
#' @return First year data and between years values
#' @export
#' @usage data.first(data.n, r, n)
#' @examples d.first<-data.first(189, 13.57751, 5)
#' @references Bardhan, D., Singh, S.R.K., Raut, A.A.and Athare, T.R. (2022). Livestock in Madhya Pradesh and Chhattisgarh: An Analysis for Some Policy Implications. Agricultural Science Digest. DOI:10.18805/ag.D-5418.


data.first<-function(data.n, r, n){
  df <- data.frame()

  for (i in 1:n) {
    data.1 = (data.n * (1 + r/100)^(-n))
    output = (data.1 * (1 + r/100)^i)
    df = rbind(df, output)
  }
  colnames(df) <- "y"
  y <- as.vector(rbind(data.1, df))
  y <- as.data.frame(y)
  Output_CAGR <- list(data.1, Values = y)
  return(Output_CAGR)
}



#' @title Log-linear Trend Model
#' @description Estimating the growth rate using the log-linear trend model
#'
#' @param data Time series data set
#' @import stats
#' @return Growth rate and model summary
#' @export
#' @usage LLTM(data)
#' @examples
#' data<- c(100,105,110,118,116,120,130)
#' g.rate <- LLTM(data)
#' @references Bardhan, D., Singh, S.R.K., Raut, A.A.and Athare, T.R. (2022). Livestock in Madhya Pradesh and Chhattisgarh: An Analysis for Some Policy Implications. Agricultural Science Digest. DOI:10.18805/ag.D-5418.

LLTM <- function(data) {
  t <- length(data)
  y <- log(data)
  x<- seq(1:t)
  ll.model <- stats::lm(y ~ x)
  s <- summary(ll.model)
  b <- unname(ll.model$coefficients[2])
  r<- (exp(b) - 1)*100

  output<- list(growth_rate = r, model_summary = s)
  return(output)
}









#' @title Computing Number of Years
#' @description Computing number of years
#'
#' @param data.1 Data of the first year
#' @param data.n Data of the last year
#' @param r CAGR
#'
#' @return Number of years and between years values
#' @export
#' @usage n.years(data.1, data.n, r)
#' @examples n.yrs<-n.years(100, 189, 13.57751)
#' @references Bardhan, D., Singh, S.R.K., Raut, A.A.and Athare, T.R. (2022). Livestock in Madhya Pradesh and Chhattisgarh: An Analysis for Some Policy Implications. Agricultural Science Digest. DOI:10.18805/ag.D-5418.


n.years<-function(data.1, data.n, r){
  df <- data.frame()
  n = (log10(data.n/data.1)/log10(1+(r/100)))
  n = round(n, digits = 0)
  for (i in 1:n) {
    output = (data.1 * (1 + r/100)^i)
    df = rbind(df, output)
  }
  colnames(df) <- "y"
  y <- as.vector(rbind(data.1, df))
  y <- as.data.frame(y)
  Output_CAGR <- list(n, Values = y)
  return(Output_CAGR)
}

Try the CAGR package in your browser

Any scripts or data that you put into this service are public.

CAGR documentation built on April 4, 2025, 4:07 a.m.