library(HistData)
library(car)
## Loading required package: carData
with(Galton,
{
sunflowerplot(parent, child, xlim = c(63, 74), ylim = c(61, 74))
reg <- lm(child ~ parent)
abline(reg)
lines(lowess(parent, child), col = "green", lwd = 2)
if(require(car)) {
dataEllipse(parent, child, xlim = c(63, 74), ylim = c(61, 74), plot.points = FALSE)
}
})
library(psych)
##
## Attaching package: 'psych'
## The following object is masked from 'package:car':
##
## logit
data(galton)
describe(galton)
## vars n mean sd median trimmed mad min max range skew
## parent 1 928 68.31 1.79 68.5 68.32 1.48 64.0 73.0 9 -0.04
## child 2 928 68.09 2.52 68.2 68.12 2.97 61.7 73.7 12 -0.09
## kurtosis se
## parent 0.05 0.06
## child -0.35 0.08
#show the scatter plot and the lowess fit
pairs.panels(galton,
main = "Galton's Parent child heights")
#but this makes the regression lines look the same
pairs.panels(galton,
lm = TRUE,
xlim = c(60, 75),
ylim = c(60, 75),
main = "Galton's Parent child heights")
#better is to scale them
pairs.panels(galton,
smooth = TRUE,
xlim = c(60, 75),
ylim = c(60, 75),
main = "Galton's Parent child heights")
with(PearsonLee,
{
lim <- c(52, 80)
xv <- seq(52, 80, .5)
sunflowerplot(parent,child, number = frequency, xlim = lim, ylim = lim, seg.col = "gray", size = .1)
abline(lm(child ~ parent, weights = frequency), col = "blue", lwd = 2)
lines(xv, predict(loess(child ~ parent, weights = frequency), data.frame(parent = xv)),
col = "red", lwd = 2)
# NB: dataEllipse doesn't take frequency into account
if(require(car)) {
dataEllipse(parent, child, xlim = lim, ylim = lim, plot.points = FALSE)
}
})
library(ggplot2)
## Registered S3 methods overwritten by 'ggplot2':
## method from
## [.quosures rlang
## c.quosures rlang
## print.quosures rlang
##
## Attaching package: 'ggplot2'
## The following objects are masked from 'package:psych':
##
## %+%, alpha
ggplot(PearsonLee, aes(x = parent, y = child, weight = frequency)) +
geom_point(size = 1.5, position = position_jitter(width = 0.2)) +
geom_smooth(method = lm,
aes(weight = frequency, colour = "Linear"),
se = FALSE, size = 1.5) +
geom_smooth(method = loess,
aes(weight = frequency, colour = "Loess"),
se = FALSE, size = 1.5) +
facet_grid(chl ~ par) +
scale_colour_manual(breaks = c("Linear", "Loess"),
values = c("green", "red")) +
theme(legend.position = c(0.14, 0.885),
legend.background = element_rect(fill = 'white'))