Functional Review

Screen Link:
https://app.dataquest.io/m/444/the-mean/9/challenge-estimating-the-population-mean

Question:
I’m having trouble with functionals in general. I have my sample sizes and I just want to be able to be able to find the mean for each sample size (need to get sample_sizes vector into the ? position).

If someone could take the time to explain how it (or functionals in general) works I would really appreciate it.

My Code:

set.seed(4)
sample_sizes <- seq(from = 5, to = 2876, by = 29)
map_dbl(sample_sizes, mean(sample(houses$SalePrice, size = ?)))
1 Like

The mean of the whole sample is: parameter <- mean(houses$SalePrice)

We create different sample sizes with sample_sizes <- seq(from = 5, to = 2876, by = 29)

We want to calculate the difference between the parameter and the mean of all the samples. We use the map_dbl to do so at once, instead of repeating the calculations one after the other.

In the code below, we pick a particular sample_sizes, say 5. We use the python lambda function style to create a function we only need for this operation function(x).

So this function takes 5 as x, supplies it to the sample() and takes a sample size of 5 for this run. The mean() is then used to average the result of the five samples of houses$SalePrice and this is subtracted from parameter .

sampling_errors <- map_dbl(sample_sizes, function(x) parameter - mean(sample(houses$SalePrice, size = x)))

See code below.

parameter <- mean(houses$SalePrice)

sample_sizes <- seq(from = 5, to = 2876, by = 29)

sampling_errors <- map_dbl(sample_sizes, function(x) parameter - mean(sample(houses$SalePrice, size = x))
                           )
1 Like

Ah so you made a custom function with size as the input. Makes sense thank you

2 Likes