## Create R functions to perform calculations (e.g., add = function(x, y) {x + y}). You can then call these functions in the 'formula' input. Press the Repeat button to repeat the simulation specified in the Simulate tab. Use functions to perform additional calculations on the repeated simulation data. Click the ? icon on the bottom left of your screen for help and examples

wealth 1000;
perc_invest 0.05;
prob_win 0.2;
win_mult 5;

win = rnd < prob_win
dyn_wealth = calc_wealth(win, wealth, win_mult)
profit = last(dyn_wealth)

## win: a vector indicating if a bet was won or lost
## wealth: amount of mony the gambler starts starts
## mult: the winning multiplier
calc_wealth <- function(win, wealth, mult) {
	wealth = c(wealth, rep(NA, length(win)))
	for (i in 1:length(win)) {
		wealth[i+1] = ifelse(
		  win[i], 
		  wealth[i] + mult * perc_invest * wealth[i], 
		  wealth[i] - perc_invest * wealth[i]
		)
	}
	return(wealth[-1])  ## dropping the initial wealth value
}

In this simulation a gambler starts with \$1,000 (`wealth`). She places a single bet each day of the year and has decided to invest 5% (`perc_invest`) of the available money each time. The probability of winning is 20% (`prob_win`) and if the gambler wins she gets 5 times (`win_mult`) the amount she bet (i.e., the investment).

```{r fig.width = 7, fig.height = 3.23, dpi = 96}
repdat <- repeater(
  simdat, 
  nr = 50, 
  vars = "rnd", 
  sum_vars = "dyn_wealth", 
  byvar = ".rep", 
  fun = "last", 
  seed = 1234
)
summary(repdat, dec = 2)
plot(repdat, custom = FALSE)
register("repdat")
```

## Diamond prices

Prices of 3,000 round cut diamonds

### Description

A dataset containing the prices and other attributes of a sample of 3000 diamonds. The variables are as follows:

### Variables

- price = price in US dollars ($338--$18,791)
- carat = weight of the diamond (0.2--3.00)
- clarity = a measurement of how clear the diamond is (I1 (worst), SI2, SI1, VS2, VS1, VVS2, VVS1, IF (best))
- cut = quality of the cut (Fair, Good, Very Good, Premium, Ideal)
- color = diamond color, from J (worst) to D (best)
- depth = total depth percentage = z / mean(x, y) = 2 * z / (x + y) (54.2--70.80)
- table = width of top of diamond relative to widest point (50--69)
- x = length in mm (3.73--9.42)
- y = width in mm (3.71--9.29)
- z = depth in mm (2.33--5.58)
- date = shipment date

### Additional information

<a href="http://www.diamondse.info/diamonds-clarity.asp" target="_blank">Diamond search engine</a>