mu3 <- c(2, 2, 1); c3 <- 1; n3 <- 300
Y3 <- matrix(rnorm(n3 * 3), ncol = 3) + rep(mu3, each = n3)
s3 <- shrink(Y3, mu3, c3) |> mutate(x = Y3[, 1], y = Y3[, 2], z = Y3[, 3], nx = x * f, ny = y * f, nz = z * f)
seg3 <- segments_xy(cbind(s3$x, s3$y, s3$z), cbind(s3$nx, s3$ny, s3$nz))
hover3 <- hover_text(s3)
# the sphere inside which shrinking hurts: center mu/2, radius sqrt(|mu|^2/4 + c/2), as a wireframe
wire <- function(center, r, n_mer = 8, n_par = 4, m = 60) {
t <- seq(0, 2 * pi, length.out = m); pieces <- list()
for (a in seq(0, pi, length.out = n_mer + 1)[-(n_mer + 1)]) # meridians
pieces[[length(pieces) + 1]] <- cbind(r * cos(t) * cos(a), r * cos(t) * sin(a), r * sin(t))
for (ph in seq(-pi / 2, pi / 2, length.out = n_par + 2)[-c(1, n_par + 2)]) # parallels
pieces[[length(pieces) + 1]] <- cbind(r * cos(ph) * cos(t), r * cos(ph) * sin(t), rep(r * sin(ph), m))
out <- do.call(rbind, lapply(pieces, function(P) rbind(P, NA)))
sweep(out, 2, center, "+")
}
W <- wire(mu3 / 2, sqrt(sum(mu3^2) / 4 + c3 / 2))
plot_ly() |>
add_trace(x = W[, 1], y = W[, 2], z = W[, 3], type = "scatter3d", mode = "lines",
line = list(color = hurt_wire, width = 3.5), hoverinfo = "none", name = "inside: shrinking hurts, if not sent to 0") |>
add_trace(x = seg3[, 1], y = seg3[, 2], z = seg3[, 3], type = "scatter3d", mode = "lines",
line = list(color = "rgba(50,50,50,0.5)", width = 2.5), hoverinfo = "none", showlegend = FALSE) |>
add_trace(data = filter(s3, !worse), x = ~x, y = ~y, z = ~z, type = "scatter3d", mode = "markers",
marker = list(color = help_col, size = 3.5, opacity = 0.85),
text = hover3[!s3$worse], hoverinfo = "text", name = "shrinking helps") |>
add_trace(data = filter(s3, worse), x = ~x, y = ~y, z = ~z, type = "scatter3d", mode = "markers",
marker = list(color = hurt_col, size = 4.5, symbol = "circle-open", line = list(color = hurt_col, width = 2)),
text = hover3[s3$worse], hoverinfo = "text", name = "shrinking hurts") |>
add_trace(x = 0, y = 0, z = 0, type = "scatter3d", mode = "markers",
marker = list(color = m_black, size = 4, symbol = "x"), name = "0 (shrink toward)", hoverinfo = "name") |>
add_trace(x = mu3[1], y = mu3[2], z = mu3[3], type = "scatter3d", mode = "markers",
marker = list(color = v_dark, size = 5, symbol = "diamond"), name = "truth mu", hoverinfo = "name") |>
layout(scene = list(aspectmode = "data",
xaxis = list(title = "", showticklabels = FALSE), yaxis = list(title = "", showticklabels = FALSE),
zaxis = list(title = "", showticklabels = FALSE),
camera = list(eye = list(x = 1.6, y = -1.5, z = 0.9))),
legend = list(x = 0.01, y = 0.99, bgcolor = "rgba(255,255,255,0.7)"),
margin = list(l = 0, r = 0, t = 0, b = 0))