Question: You are going to write some Rcpp code in this problem. ##### (a) The R code `all(x > 0L)` is not very efficient if `x`

You are going to write some Rcpp code in this problem.

##### (a) The R code `all(x > 0L)` is not very efficient if `x` is long because a temporary logical vector `x == 1` was created. Write an Rcpp function `allgt(x, y)` which takes a numeric vector `x` (assume there are no missing values) and a double `y` that checks if all the elements in `x` are greater than `y`. Benchmark the RCpp function and the R code with vectors of different proportions of positive numbers, e.g. `sample(c(1L, -1L), 1e5, prob = c(1-p, p), replace = TRUE)` for `p = 0.5, 0.05`, `0.005` and `0`. Comment on what you observe.

##### (b) Write an Rcpp function `varC(x)` which computes the sample variance of a numeric vector `x` (assume no missing values). Benchmark your function with a vector of length 1e5 and compare with `var(x)`.

Hint:

1. Use the computational formula of sample variance! 2. Sample variance is not defined if `length(x)` is 1, return `NaN` in this case.

##### (c) Similar to (b), but now `x` may contain missing values. Write an Rcpp function `varC(x, na_rm)` that mimics the built in R function `var`:

- when `x` contains missing values and `na_rm` is `FALSE`, returns `NA`; - when `x` contains missing values and `na_rm` is `TRUE`, ignores the missing values and computes the sample variance as is.

Benchmark your function and compare with `var(x, na.rm = TRUE)` by considering vectors of different lengths `n = 100, 1000, 1e4 and 1e5` and different proportions of missingness `p = 0.5, 0.05`, and `0`.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!