knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
library(jeksterslabRdatarepo)
library(knitr)
library(DT)
library(GGally)
library(plotly)
library(psych)

Load Data

data(
  quartet,
  package = "jeksterslabRdatarepo"
)

Data Table

datatable(
  data = quartet,
  options = list(
    pageLength = 11,
    dom = "t"
  )
)

Plots

pair <- 1:4
x <- paste0("x", pair)
y <- paste0("y", pair)
muhat_x <- sigmahat_x <- muhat_y <- sigmahat_y <- rhohat <- rep(x = NA, times = length(pairs))
fit <- vector(mode = "list", length = length(pair))
for (i in seq_along(pair)) {
  fit[[i]] <- lm(
    quartet[, y[i]] ~ quartet[, x[i]]
  )
  muhat_x[i] <- mean(quartet[, x[i]])
  sigmahat_x[i] <- sd(quartet[, x[i]])
  muhat_y[i] <- mean(quartet[, y[i]])
  sigmahat_y[i] <- sd(quartet[, y[i]])
  rhohat[i] <- cor(quartet[, x[i]], quartet[, y[i]])
}

Pair 1

plot_ly(
  data = quartet,
  type = "scatter",
  mode = "markers",
  x = ~x1,
  y = ~y1
) %>%
  add_markers(
    y = ~y1
  ) %>%
  add_trace(
    x = ~x1,
    y = fitted(fit[[1]]),
    mode = "lines"
  ) %>%
  layout(
    showlegend = F
  )

Pair 2

plot_ly(
  data = quartet,
  type = "scatter",
  mode = "markers",
  x = ~x2,
  y = ~y2
) %>%
  add_markers(
    y = ~y2
  ) %>%
  add_trace(
    x = ~x2,
    y = fitted(fit[[2]]),
    mode = "lines"
  ) %>%
  layout(
    showlegend = F
  )

Pair 3

plot_ly(
  data = quartet,
  type = "scatter",
  mode = "markers",
  x = ~x3,
  y = ~y3
) %>%
  add_markers(
    y = ~y3
  ) %>%
  add_trace(
    x = ~x3,
    y = fitted(fit[[3]]),
    mode = "lines"
  ) %>%
  layout(
    showlegend = F
  )

Pair 4

plot_ly(
  data = quartet,
  type = "scatter",
  mode = "markers",
  x = ~x4,
  y = ~y4
) %>%
  add_markers(
    y = ~y4
  ) %>%
  add_trace(
    x = ~x4,
    y = fitted(fit[[4]]),
    mode = "lines"
  ) %>%
  layout(
    showlegend = F
  )

Descritive Statistics

kable(
  x = data.frame(
    pair,
    muhat_x,
    sigmahat_x,
    muhat_y,
    sigmahat_y,
    rhohat
  ),
  col.names = c(
    "Pair",
    "Mean (x)",
    "Standard Deviation (x)",
    "Mean (y)",
    "Standard Deviation (y)",
    "Correlation (x, y)"
  )
)


jeksterslabds/jeksterslabRdatarepo documentation built on Jan. 5, 2021, 3:27 a.m.