LoslegenKostenlos loslegen

Covariance by hand

In R, you can quickly compute various statistical measures using functions. The interviewer might be interested if you can replicate the calculations of these functions to ensure your understanding of what the functions do.

In this exercise, you will compute covariance by hand. The covariance measures the variability of two random variables.

Recall that the formula for the covariance of a sample is:

\( \sum_{i=1}^{n} \frac{(x_i - \overline{x})(y_i - \overline{y})}{n-1}\)

The df data frame is available in your environment with variables x and y.

Diese Übung ist Teil des Kurses

Practicing Statistics Interview Questions in R

Kurs anzeigen

Anleitung zur Übung

  • Assign the number of observations in df to n.
  • Compute the covariance between x and y by hand.
  • Compute the covariance between x and y using a function from the stats package.

Interaktive Übung

Versuche dich an dieser Übung, indem du diesen Beispielcode vervollständigst.

# The number of observations
___ <- nrow(___)

# Compute covariance by hand
___((df$x-___(df$x))*(___-mean(df$y)))/(___-1)

# Compute covariance with function
___(df$x, ___)
Code bearbeiten und ausführen