How to create a Student's t Q-Q plot in R using ggplot2? | StatswithR | Arnab Hazra
Автор: StatswithR
Загружено: 2020-08-13
Просмотров: 1873
Here we explain how to generate a presentation/publication-quality Student's t Q-Q plot in R/R-studio using ggplot2. The codes for the steps explained in the video are as follows. Copy and paste them into R, run them one-by-one and try to understand what each argument is doing.
#datascience #datavisualization #visualization #ggplot2 #tidyverse #qqplot #rstudio #rcoding #studentt
Step 0a: If you don't know how to load data into R, simulate a vector X using
X = 175 + 10 * rt(1e4, df = 10)
Step 0b: if you have not installed the packages fitdistrplus, metRology, ggplot2 and qqplotr, run
install.packages("fitdistrplus"); library(fitdistrplus)
install.packages("metRology"); library(metRology)
install.packages("ggplot2"); library(ggplot2)
install.packages("qqplotr"); library(qqplotr)
Step 0c: Estimate the parameters using
par.est = fitdist(X, distr = "t.scaled", start = list(mean = mean(X), sd = sd(X), df = 30))
Step 1: ggplot(mapping = aes(sample = X)) +
stat_qq_point(distribution = "t.scaled", dparams = par.est$estimate, size = 2)
Step 2: ggplot(mapping = aes(sample = X)) +
stat_qq_point(distribution = "t.scaled", dparams = par.est$estimate, size = 2) + xlab("Theoretical Quantiles") + ylab("Sample Quantiles")
Step 3: ggplot(mapping = aes(sample = X)) +
stat_qq_point(distribution = "t.scaled", dparams = par.est$estimate, size = 2) + stat_qq_line(distribution = "t.scaled", dparams = par.est$estimate) +
xlab("Theoretical Quantiles") + ylab("Sample Quantiles")
Step 4: ggplot(mapping = aes(sample = X)) +
stat_qq_band(distribution = "t.scaled", dparams = par.est$estimate) +
stat_qq_point(distribution = "t.scaled", dparams = par.est$estimate, size = 2) + stat_qq_line(distribution = "t.scaled", dparams = par.est$estimate) +
xlab("Theoretical Quantiles") + ylab("Sample Quantiles")
Step 5: ggplot(mapping = aes(sample = X)) +
stat_qq_band(distribution = "t.scaled", dparams = par.est$estimate) +
stat_qq_point(distribution = "t.scaled", dparams = par.est$estimate, size = 2) + stat_qq_line(distribution = "t.scaled", dparams = par.est$estimate) +
xlab("Theoretical Quantiles") + ylab("Sample Quantiles") +
ggtitle("Student's t Q-Q plot of heights")
Step 6: ggplot(mapping = aes(sample = X)) +
stat_qq_band(distribution = "t.scaled", dparams = par.est$estimate) +
stat_qq_point(distribution = "t.scaled", dparams = par.est$estimate, size = 2) + stat_qq_line(distribution = "t.scaled", dparams = par.est$estimate) +
xlab("Theoretical Quantiles") + ylab("Sample Quantiles") +
ggtitle("Student's t Q-Q plot of heights") +
theme(plot.title = element_text(hjust = 0.5))
Step 7: ggplot(mapping = aes(sample = X)) +
stat_qq_band(distribution = "t.scaled", dparams = par.est$estimate) +
stat_qq_point(distribution = "t.scaled", dparams = par.est$estimate, size = 2) + stat_qq_line(distribution = "t.scaled", dparams = par.est$estimate) +
xlab("Theoretical Quantiles") + ylab("Sample Quantiles") +
ggtitle("Student's t Q-Q plot of heights") +
theme(plot.title = element_text(hjust = 0.5)) +
theme(axis.text=element_text(size=20),
axis.title=element_text(size=20),
plot.title = element_text(size=20))

Доступные форматы для скачивания:
Скачать видео mp4
-
Информация по загрузке: