W Barker
W Barker

Reputation: 312

Adstock Application: Arguments Imply Different Number of Rows

I'm new to R and trying to set up regression runs on data with an Adstock - like decay incorporated into that data.
A web search revealed an example that I have attempted to adapt for my purposes, but I'm having the problem described here in this simplified example. (There may be more problems; any comments welcome!)

# adstock calc
adstock_Nx_calc <- function(media){
  length <- length(media)
  adstock <- rep(0,length)
  for(i in 2:length){
    adstock[i] <- media[i] + (adstock[i-1]*.7)
  }
}

# Function for creating Nx test sets
create_Nxtest_sets<-function(base_p){

  # 10 weeks of data
  week <- 1:10

  # Base sales of base_p units
  base<-rep(base_p,10)

  # set up NxE parameters
  NxE <- c(24,14,33,19,32,10,5,15,12,1)

  # set up adstock
  adstock <- adstock_Nx_calc(NxE)

  # sales equation
  sales <- base + NxE + adstock

  # set up output data.frame
  output<-data.frame(week, sales, NxE, adstock)
  output  
}

test <- create_Nxtest_sets(base_p=1500)

The resulting error is "arguments imply different number of rows: 10, 0" I've searched both generally and in SO here and here, but these don't seem to be related. I guess the error means that adstock is not calculating and is therefore NULL, but I cannot figure out why; any help would be appreciated.

Upvotes: 0

Views: 285

Answers (1)

Ista
Ista

Reputation: 10437

You need to return adstock from adstock_Nx_calc, like this:

adstock_Nx_calc <- function(media){
  length <- length(media)
  adstock <- rep(0,length)
  for(i in 2:length){
    adstock[i] <- media[i] + (adstock[i-1]*.7)
  }
  return(adstock)
}

Upvotes: 1

Related Questions