Assignment 3
1) Create an R file named `myFuncs.R`, which contains definitions for the following two functions.
1a) Write an R function, called `pos.mean`, which takes 1 argument `x`, a vector of numeric values. The function should calculate and return the mean of positive values in the vector `x` using the formula:
$$ \frac1{n^2}\sum_{i=1}^n(n \cdot x_i) $$
where `n` is the length of the vector `x` and `xi` are elements of `x`. Be sure to calculate the mean exactly as indicated in the equation above. Do not simplify the expression.
There are native R functions `sum()` and `length()` that you may find useful; use `help()` to understand how to use them. You should also know that the caret `^` symbol is used to raise something to a power, as well as the double star `**`. That is, 2^3 == 2**3 == 8.
1b) Write an R function, called `neg.median`, which also takes 1 argument `x`, a vector of numeric values. The function should calculate and return the median of negative values in the vector `x`. In this case the numeric values in `x`, should be filtered for being negative; all other values should be ignored.
The algorithm of finding the median value in the vector you need to implement is as follows:
- sort the elements in the vector (use the built-in R function `sort()` for this);
- if the vector has odd number of elements, the median is the middle element of the sorted vector;
- if the number of elements is even, take an average of two elements in the middle of vector
The "%%" operator may be helpful. Do not use R's built-in "median" function.
Example:
- in the vector `1, 2, 3, 4, 5, 6, 7` number 4 is in the middle of the vector and therefore is the median;
- in the vector `1, 2, 3, 4, 5, 6, 7, 8` elements 4 and 5 are in the middle (since the number of elements in the vector is even), therefore the median is an average of these elements: (4+5)/2 = 4.5.
Find ways to use the techniques explained in class, such as 'slicing' (lecture 4, slide 9), which will make your code cleaner, more robust and efficient. No loops should be used for either of these two functions.
The `source` command gives you access to the functions stored in a file. The `source` command reads and executes the whole file as if you were typing the lines at the R prompt. Use the `source("myFuncs.R")` command inside your R prompt (RGui if you have Windows) to check the output of your functions. Your functions should behave as follows:
>
> source("myFuncs.R")
>
> pos.mean(c(-1, 3, -0.4, -2.5, 4, -3.1))
[1] 3.5
>
> pos.mean(c(-1, 3, -0.4, -2.5, 4, -3.1, 1))
[1] 2.666667
>
> neg.median(c(-1, 3, -0.4, -2.5, 4, -3.1))
[1] -1.75
>
> neg.median(c(-1, 3, -0.4, -2.5, 4, -3.1, -1))
[1] -1
>
2) Write a function, called `LehmerMean`, which takes 2 arguments, `x`, a vector of numeric values, and `p`, a numeric value. The function should calculate and return the Lehmer mean of `x`, for value `p`.
The Lehmer mean determines the typical value of a set of numbers by calculating the sum of the elements of `x` raised to the power `p`, divided by the sum of the elements of `x` raised to the power `p - 1`. Thus for `k` numbers, i.e., for a set of numbers x1, x2, ..., xk, the Lehmer mean is defined as
$$ L_p(x) = \frac{\sum_{i=1}^k x^p_k}{\sum_{i=1}^k x^{p-1}_k}$$
The values of `x` must be positive (greater than zero), according to the definition of the Lehmer mean. Your function should remove those values of `x` which are not positive, and calculate the mean on the remaining values of `x`. Do not try to find R packages that calculate the Lehmer mean. Write a function that calculates the Lehmer mean yourself!
Use the techniques explained in class, such as 'slicing', which will make your code cleaner, more robust and efficient. No loops should be used. Your function should behave as follows:
>
> source("myFuncs.R")
>
> test.vec <- c(3, 4, 12, -1, 8, 23, 22, 0, -11:-6)
>
> LehmerMean(1:10, 3)
[1] 7.857143
>
> LehmerMean(test.vec, 2)
[1] 17.30556
>
Be sure to comment your code, indent your code blocks, and use meaningful variable names. You should not need to use loops in this assignment.
Submit your script myFuncs.R.
Assignments will be graded on 10 points basis.
Due date is October 5th 2023 (midnight), with 0.5 point penalty per day for late submission until the cut-off date of October 12th, 2023, at 9:00 am.