The Mean - Screen 14 & The Weighted Mean, Screen 4

  1. The below code geom vline = length(sampling_errors) should be length( sample size).
    Pls. correct.

Generating a scatter plot to represent visually how the sampling error changes as the sample size increases:
library(ggplot2) ggplot(data = df, aes(x = sample_sizes, y = sampling_errors)) + geom_point() + geom_hline(yintercept = 0) + **geom_vline(xintercept = **length(sampling_errors)**)** + labs(x = "Sample size", y = "Sampling error")

  1. In the code below, I didn’t understand the continuity :
    compute_weighted_mean <- function(distribution, weights) {
    weighted_distribution <- purrr::map2_dbl(distribution, weights, function(x, y) x * y)

    sum(weighted_distribution) / sum(weights)

Is sum(weighted_distribution/sum(weights) a separate line code.
Why function(x,y) is again used inside the main func().
We just have to calculate the dot product x*y & divide by sum(weights)

PS: I am new to coding & have only started in this course.

Hello @sharathnandalike,

We will fix it. Thank you!

You’re right! The simplest way to do it is x*y over sum(weights), but for the sake of learning, we want to use a for-loop-like structure to compute it as we did in the mean mission; being close to the weighted mean mathematical formula. We will clarify this or make it simple as you suggest.

FYI, purrr::map2_dbl() is a mapper function from the package purrr and it imitates a for-loop with two inputs and the function(x, y) is what we call anonymous function. This piece of code is equivalent to:

weighted_distribution <- 0
for (i in 1:length(distribution)) {
    x <- distribution[i]
    y <- weights[i]
    weighted_distribution <- x*y

We teach both mapper functions and anonymous functions in our content (checks references).