Mean carried forward
Eine Alternative zu "last observation carried forward" ist, NAs durch den Mittelwert aller vorherigen Nicht-NA-Werte zu ersetzen. Das nennt sich mean carried forward. Auch hier gilt: In R musst du dich zwischen Lesbarkeit und Geschwindigkeit entscheiden. Der folgende Code ist auf Lesbarkeit ausgelegt:
na_meancf1 <- function(x) {
total_not_na <- 0
n_not_na <- 0
res <- x
for(i in seq_along(x)) {
if(is.na(x[i])) {
res[i] <- total_not_na / n_not_na
} else {
total_not_na <- total_not_na + x[i]
n_not_na <- n_not_na + 1
}
}
res
}
Die iterative Struktur macht eine Vektorisierung schwierig. Stattdessen übertragen wir das nach C++. Vervollständige die Definition von na_meancf2(), einer C++-Übersetzung von na_meancf1().
Diese Übung ist Teil des Kurses
R-Code mit Rcpp optimieren
Anleitung zur Übung
- Prüfe in der
if-Bedingung, ob dasi-te Element vonxeinemNAeinesNumericVectorentspricht. - Wenn die Bedingung erfüllt ist, setze das
i-te Ergebnis auf die Summe der nicht fehlenden Werte,total_not_na, geteilt durch die Anzahl der nicht fehlenden Werte,n_not_na. - Andernfalls erhöhe
total_not_naum dasi-te Element vonxund addiere1zun_not_na.
Interaktive Übung
Vervollständige den Beispielcode, um diese Übung erfolgreich abzuschließen.
#include
using namespace Rcpp;
// [[Rcpp::export]]
NumericVector na_meancf2(NumericVector x) {
double total_not_na = 0.0;
double n_not_na = 0.0;
NumericVector res = clone(x);
int n = x.size();
for(int i = 0; i < n; i++) {
// If ith value of x is NA
if(___) {
// Set the ith result to the total of non-missing values
// divided by the number of non-missing values
res[i] = ___ / ___;
} else {
// Add the ith value of x to the total of non-missing values
___;
// Add 1 to the number of non-missing values
___;
}
}
return res;
}
/*** R
library(microbenchmark)
set.seed(42)
x <- rnorm(1e5)
x[sample(1e5, 100)] <- NA
microbenchmark(
na_meancf1(x),
na_meancf2(x),
times = 5
)
*/