Skip to content

Instantly share code, notes, and snippets.

@graebnerc
Created April 19, 2024 12:03
Show Gist options
  • Save graebnerc/908de56b52a28fda6fe5acb14393942d to your computer and use it in GitHub Desktop.
Save graebnerc/908de56b52a28fda6fe5acb14393942d to your computer and use it in GitHub Desktop.
The example of a function definition discussed in the recap session on April 19.
# A step-by-step solution for the first function task of the "Basics" tutorial
# Goal: define a function that computes the sample variance of a vector
# Note: there are many strategies to develop functions; here we start by first
# writing the code that solves our problem for one particular case outside the
# function, and then generalize this code in a function.
# First step: think about the starting point for your function. In our case:
# we start with a vector containing some numbers, because this is from what
# we compute the variance in the first place:
example_vector <- c(1,2,3,4)
# Second step: break the formula for the sample variance into parts, and
# translate each part into code. We start with the numerator of the formula:
vector_mean <- mean(example_vector) # The mean of the vector
vector_mean
vector_deviations <- example_vector - vector_mean # Deviation of each element from the mean
vector_deviations
vector_deviations_squared <- vector_deviations**2 # The squared deviations
vector_deviations_squared
numerator <- sum(vector_deviations_squared) # The sum of the squared deviations
numerator
# Now that we have computed the numerator, lets move to the denominator of the
# fraction:
nb_elements_vector <- length(example_vector) # This is the 'n' in the equation
denominator <- nb_elements_vector - 1
# To get the overall result, just divide the numerator by the denominator:
result <- numerator / denominator
result
# Third step: generalize our solution for the particular case into a general
# function. To this end, think of a function name (here: 'var_manual') and
# think about the number of arguments this function needs (here: one, i.e.
# the vector for which we want to compute the variance):
var_manual <- function(x){
print(x)
}
var_manual(example_vector)
# So far, the function only prints the argument we give to it. Now copy paste
# our code from above, and replace the name 'example_vector' with the name
# of our argument (here: x). If we did not do so, the function would only work
# if 'example_vector' was defined outside the function, and it would always
# return the same result no matter what input we provide as an argument.
var_manual <- function(x){
# Numerator:
vector_mean <- mean(x) # The mean of the vector
vector_deviations <- x - vector_mean # Deviation of each element from the mean
vector_deviations_squared <- vector_deviations**2 # The squared deviations
numerator <- sum(vector_deviations_squared) # The sum of the squared deviations
# Denominator:
nb_elements_vector <- length(x) # This is the 'n' in the equation
denominator <- nb_elements_vector - 1
# Result:
result <- numerator / denominator
result
}
# Now we can use the function with arbitrary vectors as input. In the following
# three examples, the same procedure from within the function was applied to
# three different vectors. The variable 'x' within the function can be thought
# of as a placeholder that is replaced by the function input:
var_manual(x = c(1, 2, 3, 4, 5, 99))
var_manual(x = c(-4, 2, 4, 8, 10))
var_manual(x = c(50, 100, 82, 33))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment