Skip to content

Instantly share code, notes, and snippets.

@steveharoz
Last active October 29, 2024 13:09
Show Gist options
  • Save steveharoz/32253b0df510db71a131634461bd3df3 to your computer and use it in GitHub Desktop.
Save steveharoz/32253b0df510db71a131634461bd3df3 to your computer and use it in GitHub Desktop.
Measurement Error Quartet (based on Michael Friendly's post)
library(tidyverse)
library(patchwork)
library(glue)
set.seed(7)
data = tibble(
x = rnorm(50, 5),
y = x + rnorm(50, 0, .7),
x_noise = x + rnorm(50, 0, 1),
y_noise = y + rnorm(50, 0, 1)
)
cor_no_no = cor.test(data$x, data$y)
cor_no_yes = cor.test(data$x, data$y_noise)
cor_yes_no = cor.test(data$x_noise, data$y)
cor_yes_yes = cor.test(data$x_noise, data$y_noise)
ggplot(data) +
aes(x=x, y=y) +
geom_point(size=2, alpha = 0.5) +
geom_smooth(method = "lm", color="red", fill="red", fullrange = TRUE) +
expand_limits(x = c(1, 9), y = c(1, 9)) +
coord_cartesian(xlim = c(1, 9), ylim = c(1, 9)) +
theme_minimal() +
labs(title = glue("none\nr = {round(cor_no_no$estimate, 2)} [{round(cor_no_no$conf.int[[1]], 2)}, {round(cor_no_no$conf.int[[2]], 2)}]")) +
ggplot(data) +
aes(x=x, y=y_noise) +
geom_point(size=2, alpha = 0.5) +
geom_smooth(method = "lm", color="red", fill="red", fullrange = TRUE) +
expand_limits(x = c(1, 9), y = c(1, 9)) +
coord_cartesian(xlim = c(1, 9), ylim = c(1, 9)) +
theme_minimal() +
labs(title = glue("y noise\nr = {round(cor_no_yes$estimate, 2)} [{round(cor_no_yes$conf.int[[1]], 2)}, {round(cor_no_yes$conf.int[[2]], 2)}]")) +
ggplot(data) +
aes(x=x_noise, y=y) +
geom_point(size=2, alpha = 0.5) +
geom_smooth(method = "lm", color="red", fill="red", fullrange = TRUE) +
expand_limits(x = c(1, 9), y = c(1, 9)) +
coord_cartesian(xlim = c(1, 9), ylim = c(1, 9)) +
theme_minimal() +
labs(title = glue("x noise\nr = {round(cor_yes_no$estimate, 2)} [{round(cor_yes_no$conf.int[[1]], 2)}, {round(cor_yes_no$conf.int[[2]], 2)}]")) +
ggplot(data) +
aes(x=x_noise, y=y_noise) +
geom_point(size=2, alpha = 0.5) +
geom_smooth(method = "lm", color="red", fill="red", fullrange = TRUE) +
expand_limits(x = c(1, 9), y = c(1, 9)) +
coord_cartesian(xlim = c(1, 9), ylim = c(1, 9)) +
theme_minimal() +
labs(title = glue("x and y noise\nr = {round(cor_yes_yes$estimate, 2)} [{round(cor_yes_yes$conf.int[[1]], 2)}, {round(cor_yes_yes$conf.int[[2]], 2)}]"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment