Good Day
I am wondering why this code is not running. I was new in R and performing cash flow simulation.
title: "Cash Flow Simulation"
author: "Aris Zoleta"
date: "`r Sys.Date()`"
output: html_document
---
# Introduction
This markdown explains the code used to run the simulation for the estimation of the discounted cash flow and enterprise value.
```{r}
# Define variables
revenue <- 100 # Mean revenue
sd_revenue <- 0.1 # Standard deviation of revenue
cogs_ratio <- 0.6 # Cost of goods sold as a percentage of revenue
opex_ratio <- 0.2 # Operating expenses as a percentage of revenue
tax_rate <- 0.3 # Tax rate
risk_free_rate <- 0.03 # Risk-free rate
beta <- 1.2 # Beta
market_return <- 0.1 # Market return
num_simulations <- 1000 # Number of simulations
num_years <- 5 # Number of years
# Create a matrix to store results
cf_matrix <- matrix(0, nrow=num_simulations, ncol=num_years)
# Simulate cash flows
for (i in 1:num_simulations) {
# Simulate revenue
revenue_sim <- rnorm(num_years, revenue, sd_revenue*revenue)
# Calculate cost of goods sold
cogs <- cogs_ratio * revenue_sim
# Calculate operating expenses
opex <- opex_ratio * revenue_sim
# Calculate EBIT
ebit <- revenue_sim - cogs - opex
# Calculate taxes
taxes <- tax_rate * ebit
# Calculate net income
net_income <- ebit - taxes
# Calculate cash flow
cf <- net_income + cogs
# Store results
cf_matrix[i,] <- cf
}
# Calculate cost of equity
cost_of_equity <- risk_free_rate + beta * (market_return - risk_free_rate)
# Calculate the terminal value
terminal_cf <- cf_matrix[,num_years] * (1 + sd_revenue) / (cost_of_equity - sd_revenue)
## From Here my code is not running ###
# Calculate the present value of cash flows
pv_cf <- apply(cf_matrix, 2, function(x) present_value(x, cost_of_equity))
# Calculate the present value of the terminal value
pv_terminal <- present_value(terminal_cf, cost_of_equity^num_years)
# Calculate the total equity value
equity_value <- sum(pv_cf) + pv_terminal
# Print results
cat("Equity value:", round(equity_value, 2), "\n")