I am relatively new to R, and I'm getting an error when I'm trying to run some functions within a package written by Facebook marketing science (Simmmulator): https://github.com/facebookexperimental/siMMMulator/blob/main/README.md
Step 0 runs absolutely fine:
# Copyright (c) Meta Platforms, Inc. and affiliates.
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
####################################################################
#' Step 0 : Define Basic Parameters
#'
#' User inputs basic parameters that will be used in subsequent steps to simulate the data set, exists so that user does not have to keep inputting them. The parameters here initialize some variables in the user's environment.
#'
#' @param years A number, number of years you want to generate data for. Must be a whole number and equal to or greater than 1.
#' @param channels_impressions A vector of character strings, names of media channels that use impressions as their metric of activity (Examples: Facebook, TV, Long-Form Video), must be in vector format with strings. Do not provide if not applicable to you.
#' @param channels_clicks A vector of character strings, names of media channels that use clicks as their metric of activity (Examples: Search), must be in vector format with strings. Do not provide if not applicable to you.
#' @param frequency_of_campaigns A number, how often campaigns occur (for example, frequency of 1 would yield a new campaign every 1 day with each campaign lasting 1 day). Must be a whole number greater than or equal to 1.
#' @param true_cvr A vector of numbers, what the underlying conversion rates of all the channels are, statistical noise will be added on top of this, should be a vector of numbers between 0 and 1 in the SAME order as how channels were specified (channels that use impressions first, followed by channels that use clicks), must have same length as number of channels
#' @param revenue_per_conv A number, How much money we make from a conversion (i.e. profit from a unit of sale). Must be a number greater than 0.
#' @param start_date A string in the format yyyy/mm/dd that determines when your daily data set starts on.
#'
#' @return A list (in users' global environment) of variables the user has input
#' @export
#'
#' @examples
#' step_0_define_basic_parameters(years = 5,
#' channels_impressions = c("Facebook", "TV", "Long-Form Video"),
#' channels_clicks = c("Search"),
#' frequency_of_campaigns = 1,
#' true_cvr = c(0.001, 0.002, 0.01, 0.005),
#' revenue_per_conv = 1,
#' start_date = "2017/1/1")
step_0_define_basic_parameters <- function(years = 5,
channels_impressions = c(),
channels_clicks = c(),
frequency_of_campaigns = 1,
true_cvr = c(0.001, 0.002, 0.01, 0.005),
revenue_per_conv = 1,
start_date = "2017/1/1"){
years_var <- years
channels_impressions_var <- channels_impressions
channels_clicks_var <- channels_clicks
frequency_of_campaigns_var <- frequency_of_campaigns
true_cvr_var <- true_cvr
revenue_per_conv_var <- revenue_per_conv
start_date <- as.Date(start_date)
# Display error messages for invalid inputs
if (typeof(years_var) != "double") stop("You did not enter a number for years. You must have years be a numeric type.") # error if incorrect variable type for years
if (typeof(frequency_of_campaigns) != "double") stop("You did not enter a number for frequency_of_campaigns. You must enter a numeric") # error if incorrect variable type for frequency
if (typeof(true_cvr) != "double") stop("You did not enter a number for the true conversion rates. Must enter a numeric." )# error if incorrect variable type for true_cvr
if (typeof(revenue_per_conv) != "double") stop("You did not enter a number for the revenue per conversion. Must enter a numeric." ) # error if incorrect variable type for revenue_per_conversion
if (years_var < 1) stop('You entered less than 1 year. Must generate at least 1 year worth of data') # error if less than 1 year
if ((years_var%%1==0) == FALSE) stop("You entered a decimal for the number of years. Must choose a whole number of years") # error if number of years is a decimal
if (length(true_cvr)!= sum(!is.na(channels_impressions)) + sum(!is.na(channels_clicks))) stop("Did not input in enough numbers or input in too many numbers for conversion rates. Must have a conversion rate for each channel specified.") # error if not enough conversion rates are supplied
if (ifelse(all(true_cvr > 0), TRUE, FALSE) == FALSE) stop("You entered a negative conversion rate. Enter a conversion rate between 0 and 1") # error if any conversion rates entered are less than 0
if (ifelse(all(true_cvr <= 1), TRUE, FALSE) == FALSE) stop("You entered a conversion rate greater than 1. Enter conversion rate between 0 and 1.") # error if any conversion rates are greater than 1
if ((frequency_of_campaigns %% 1 == 0) == FALSE) stop("You entered a decimal for the frequency of campaigns. You must enter a whole number") # error if frequency of campaign is not an integer
if (frequency_of_campaigns < 1) stop ("You entered a frequency of campaign less than 1. You must enter a number greater than 1") # error if frequency of campaign is < 1
if (revenue_per_conv <= 0) stop("You entered a negative or zero revenue per conversion. You must enter a positive number") # error if revenue_per_conv is <= 0
if (typeof(start_date) != "double") stop("You've didn't enter a correct format for the date. Enter as a string yyyy/mm/dd")
# return variables as outputs to use with other functions
list_of_vars <- list(years_var,
channels_impressions_var,
channels_clicks_var,
frequency_of_campaigns_var,
true_cvr_var,
revenue_per_conv_var,
start_date)
# print them out so people can see
print("You have just run step 0: Defining Basic Parameters")
print("To confirm what you have input: ")
print(paste("Years of Data to generate : ", list_of_vars[[1]]))
print(paste("Channel that use impressions : ", sapply(list_of_vars[[2]], paste, collapse = "")))
print(paste("Channel that use clicks : ", sapply(list_of_vars[[3]], paste, collapse = "")))
print(paste("How frequently campaigns occur : ", list_of_vars[[4]]))
print(paste("True CVRs of a channel (in order of channels you specified) : ", sapply(list_of_vars[[5]], paste, collapse = "")))
print(paste("Revenue per conversion : ", list_of_vars[[6]]))
print(paste("Date the data set will start with : ", list_of_vars[[7]]))
return(list_of_vars)
}
Step 1 generates the error: Error in step_1_create_baseline() : promise already under evaluation: recursive default argument reference or earlier problems?
# Copyright (c) Meta Platforms, Inc. and affiliates.
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
####################################################################
#' Step 1 : Simulate Daily Baseline Sales
#'
#' Generates daily baseline sales (sales not due to ad spend) for number of years specified. Takes user input and ads statistical noise.
#'
#' @param my_variables A list that was created after running step 0. It stores the inputs you've specified.
#' @param base_p A number, Amount of baseline sales we get in a day (sales not due to ads)
#' @param trend_p A number, How much baseline sales is going to grow over the whole period of our data.
#' @param temp_var A number, How big the height of the sine function is for temperature -- i.e. how much temperature varies (used to inject seasonality into our data)
#' @param temp_coef_mean A number, The average of how important seasonality is in our data (the larger this number, the more important seasonality is for sales)
#' @param temp_coef_sd A number, The standard deviation of how important seasonality is in our data (the larger this number, the more variable the importance of seasonality is for sales)
#' @param error_std A number, Amount of statistical noise added to baseline sales (the larger this number, the noisier baseline sales will be).
#'
#' @return A data frame with daily baseline sales
#' @importFrom stats rnorm
#' @export
#'
#' @examples
#' \dontrun{
#' step_1_create_baseline(my_variables = my_variables,
#' base_p = 10000,
#' trend_p = 1.8,
#' temp_var = 8,
#' temp_coef_mean = 50000,
#' temp_coef_sd = 5000,
#' error_std = 100)
#' }
step_1_create_baseline <- function(my_variables = my_variables,
base_p = 10000,
trend_p = 2,
temp_var = 8,
temp_coef_mean = 50000,
temp_coef_sd = 5000,
error_std = 100){
# Extract necessary variables from Step 0's output
years = my_variables[[1]][[1]]
# Display error messages for invalid inputs
if (typeof(base_p) != "double") stop("You did not enter a number for base_p. You must have years be a numeric type.") # error if incorrect variable type for base_p
if (typeof(trend_p) != "double") stop("You did not enter a number for trend_p. You must enter a numeric") # error if incorrect variable type for trend_p
if (typeof(temp_var) != "double") stop("You did not enter a number for temp_var. Must enter a numeric." )# error if incorrect variable type for temp_var
if (typeof(temp_coef_mean) != "double") stop("You did not enter a number for temp_coef_mean. Must enter a numeric." ) # error if incorrect variable type for temp_coef_mean
if (typeof(temp_coef_sd) != "double") stop("You did not enter a number for temp_coef_sd. Must enter a numeric." ) # error if incorrect variable type for temp_coef_sd
if (typeof(error_std) != "double") stop("You did not enter a number for error_std. Must enter a numeric." ) # error if incorrect variable type for error_std
if (error_std > base_p) warning("You entered an error much larger than your baseline sales. As a result, you may get negative numbers for baseline sales. We have corrected these negative baseline sales to 0. To not get this warning, set an error_std much lower than base_p.")
# Number of days to generate data for
day <- 1:(years*365)
# Base sales of base_p units
base <- rep(base_p,years*365)
#Trend of trend_p extra units per day
trend_cal <- (trend_p/(years*365))*base_p
trend <- trend_cal*day
#Temperature generated by a sin function and we can manipulate how much the sin function goes up or down with temp_var
temp <- temp_var*sin(day*3.14/182.5)
# coefficient of temperature's effect on sales will be a random variable with normal distribution
seasonality <- rnorm(1, mean = temp_coef_mean, sd = temp_coef_sd)*temp
# add some noise to the trend
error <- rnorm(years*365, mean=0, sd=error_std)
# Generate series for baseline sales
baseline_sales <- base + trend + seasonality + error
# if error term makes baseline_sales negative, make it 0
for(i in 1:length(baseline_sales)) {
if(baseline_sales[i] < 0) {baseline_sales[i] <- 0 }
}
output <- data.frame(day, baseline_sales, base, trend, temp, seasonality, error)
print("You have completed running step 1: generating baseline sales.")
return(output)
}
The error suggests that somewhere as variable is being assigned as itself, and the only place I can see that happenings is
my_variables = my_variables
But that brings a broader question which is that I can't see anywhere where my_variables is defined in the first place.
Really appreciate any support, am sure it's something stupid.
my_variables
, and step 1 uses it. It's their bad programming style to use a parameter as its own default value, but things will probably work if you follow the instructions and specify it, don't rely on the default.