# Nuisance observations
set.seed(20190514)
x <- (runif(20)-0.5)*4
y <- x
d1 <- data.frame(x1 = c(rnorm(50, -3),
rnorm(50, 3), x),
x2 = c(rnorm(50, -3),
rnorm(50, 3), y),
cl = factor(c(rep("A", 50),
rep("B", 70))))
d1 <- d1 |>
mutate_if(is.numeric, function(x) (x-mean(x))/sd(x))
# Nuisance variables
set.seed(20190512)
d2 <- data.frame(x1=c(rnorm(50, -4),
rnorm(50, 4)),
x2=c(rnorm(100)),
cl = factor(c(rep("A", 50),
rep("B", 50))))
d2 <- d2 |>
mutate_if(is.numeric, function(x) (x-mean(x))/sd(x))
d1_hs <- hclust(dist(d1[,1:2]),
method="single")
d1 <- d1 |>
mutate(cls = factor(cutree(d1_hs, 2)))
pc_d1s <- ggplot(d1) +
geom_point(aes(x=x1, y=x2, colour=cls),
size=2, alpha=0.8) +
scale_colour_discrete_divergingx(palette = "Zissou 1",
nmax=4, rev=TRUE) +
ggtitle("Result 1") +
theme(legend.position="none")
d1_hw <- hclust(dist(d1[,1:2]),
method="ward.D2")
d1 <- d1 |>
mutate(clw = factor(cutree(d1_hw, 2)))
pc_d1w <- ggplot(d1) +
geom_point(aes(x=x1, y=x2, colour=clw),
size=2, alpha=0.8) +
scale_colour_discrete_divergingx(palette = "Zissou 1",
nmax=4, rev=TRUE) +
ggtitle("Result 2") +
theme(legend.position="none")
pc_d1s + pc_d1w +
plot_layout(ncol=2)