class: center, middle, inverse, title-slide .title[ # Calculating the covariance ] --- <style type="text/css"> .left-column, .right-column { display: inline-block; vertical-align: top; width: 46%; padding: 0 1%; } .left-column pre { font-size: 85%; max-height: 400px; overflow-y: auto; } </style> **Covariance as the mean of cross products** Before breaking this down, I show the whole game: `\(\Sigma{(X_i-\overline{X})(Y_i-\overline{Y})}/(n-1)\)` ``` r gc_rils |> filter(!is.na(log10_petal_area_mm), !is.na(prop_hybrid)) |> mutate(dev_x = log10_petal_area_mm - mean(log10_petal_area_mm)) |> mutate(dev_y = prop_hybrid - mean(prop_hybrid)) |> mutate(cross_prod = dev_x* dev_y) |> summarise(sumXprod = sum(cross_prod), n = n(), covar = sumXprod/ (n-1)) ``` | sumXprod| n| covar| |--------:|--:|------:| | 0.7413| 99| 0.0076| --- class: center middle section-slide # Let's work through this! --- **Find means** ``` r gc_rils |> filter(!is.na(log10_petal_area_mm), !is.na(prop_hybrid))|> select(x = log10_petal_area_mm, y = prop_hybrid ) |> mutate(mean_x = mean(x)) |> mutate(mean_x = mean(y)) ```
--- **Find deviations** ``` r gc_rils |> filter(!is.na(log10_petal_area_mm), !is.na(prop_hybrid))|> select(x = log10_petal_area_mm, y = prop_hybrid ) |> mutate(mean_x = mean(x)) |> mutate(dev_x = x - mean(x)) |> mutate(mean_x = mean(y)) |> mutate(dev_y = y - mean(y)) ```
--- **Find cross products** ``` r gc_rils |> filter(!is.na(log10_petal_area_mm), !is.na(prop_hybrid))|> select(x = log10_petal_area_mm, y = prop_hybrid ) |> mutate(mean_x = mean(x)) |> mutate(dev_x = x - mean(x)) |> mutate(mean_x = mean(y)) |> mutate(dev_y = y - mean(y)) |> mutate(cross_prod = dev_x - dev_y) ```
--- **Summing cross products** ``` r gc_rils |> filter(!is.na(log10_petal_area_mm), !is.na(prop_hybrid))|> select(x = log10_petal_area_mm, y = prop_hybrid ) |> mutate(mean_x = mean(x)) |> mutate(dev_x = x - mean(x)) |> mutate(mean_x = mean(y)) |> mutate(dev_y = y - mean(y)) |> mutate(cross_prod = dev_x * dev_y) |> summarise(sum_cross_prod = sum(cross_prod)) ``` | sum_cross_prod| |--------------:| | 0.7413| --- **Dividing by `\((n - 1)\)`** ``` r gc_rils |> filter(!is.na(log10_petal_area_mm), !is.na(prop_hybrid))|> select(x = log10_petal_area_mm, y = prop_hybrid ) |> mutate(mean_x = mean(x)) |> mutate(dev_x = x - mean(x)) |> mutate(mean_x = mean(y)) |> mutate(dev_y = y - mean(y)) |> mutate(cross_prod = dev_x * dev_y) |> summarise(sum_cross_prod = sum(cross_prod), covar = sum_cross_prod / (n()-1)) ``` | sum_cross_prod| covar| |--------------:|------:| | 0.7413| 0.0076| --- class: center middle section-slide # *'Fin'*