User Tools

Site Tools


b:head_first_statistics:visualization

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
b:head_first_statistics:visualization [2024/09/04 08:36] – [Scatter plot] hkimscilb:head_first_statistics:visualization [2025/09/08 08:22] (current) – [Histogram Modality] hkimscil
Line 91: Line 91:
 </code> </code>
 {{:b:head_first_statistics:pasted:20240904-082258.png}} {{:b:head_first_statistics:pasted:20240904-082258.png}}
 +
 +<code>
 +dat.iq <- rnorm(1000, 100, 15)
 +head(dat.iq)
 +tail(dat.iq)
 +head(dat.iq, n=12)
 +tail(dat.iq, n=12)
 +
 +mean(dat.iq)
 +sd(dat.iq)
 +
 +hist(dat.iq)
 +hist(dat.iq, breaks=30, col='lightblue')
 +
 +set.seed(101)
 +dat.iq <- rnorm(1000, 100, 15)
 +head(dat.iq)
 +tail(dat.iq)
 +head(dat.iq, n=12)
 +tail(dat.iq, n=12)
 +
 +mean(dat.iq)
 +sd(dat.iq)
 +
 +hist(dat.iq)
 +hist(dat.iq, breaks=30, col='lightblue')
 +</code>
 ====== Scatter plot ====== ====== Scatter plot ======
 <code> <code>
Line 152: Line 179:
 {{:b:head_first_statistics:pasted:20240904-083157.png}} {{:b:head_first_statistics:pasted:20240904-083157.png}}
  
-A bit more fancy line  +Outlier에 대한 주의 
-<code># Enhanced Scatterplot of MPG vs. Weight +[{{:pearson-6.png? |}}]
-# by Number of Car Cylinders +
-library(car) +
-scatterplot(mpg ~ wt | cyl, data=mtcars, +
-   xlab="Weight of Car", ylab="Miles Per Gallon", +
-   main="Enhanced Scatter Plot", +
-   labels=row.names(mtcars))</code> +
-{{:c:ps1-1:2019:pasted:20190909-080032.png}} +
 <WRAP clear /> <WRAP clear />
  
-see + 
 +====== Presentation ====== 
 +For a very good example, see
 https://www.gapminder.org/answers/how-does-income-relate-to-life-expectancy/ https://www.gapminder.org/answers/how-does-income-relate-to-life-expectancy/
   * Life expectancy data: {{:life.exp.csv}}   * Life expectancy data: {{:life.exp.csv}}
  
 +<WRAP clear/>
 +====== Histogram skewedness ======
 +<WRAP column half>
 <code> <code>
-le <as.data.frame(read.csv("http://commres.net/wiki/_media/life.exp.csv", header=T)) +#### 
-colnames(le)[1] <- "c.code# not really necessary. But, sometimes imported first characters are broken. +# left-skewed distribution 
-lea <- le$X2017 +# 1. 
-leb <- lea[complete.cases(lea)] +set.seed(1) 
-hist(lebcolor="grey")+data <- rbeta(500, shape1 = 10, shape2 = 2) 
 +hist(data, probability = TRUE,  
 +     main = "Histogram with Left-skewed data", 
 +     xlab "Value", ylab = "Density",  
 +     col = "lightblue", border = "white") 
 + 
 +# 2. 
 +# install.packages("fitdistrplus")  
 +library(fitdistrplus) 
 + 
 +fit <- fitdist(data, "beta") 
 +alpha_est <- fit$estimate["shape1"] 
 +beta_est <- fit$estimate["shape2"
 + 
 +# 3. 
 +curve(dbeta(x, shape1 = alpha_est, shape2 = beta_est), 
 +      add = TRUEcol = "red", lwd = 2)
 </code> </code>
 +</WRAP>
  
-[{{:c:ps1-1:2019:pasted:20190909-110252.png|Life expectancy in 2017}}] +<WRAP column half> 
-<WRAP clear/>+{{:b:head_first_statistics:pasted:20250903-074821.png}} 
-[{{:c:ps1-1:2019:pasted:20190909-104759.png|Distribution of temperature}}] +</WRAP> 
-<WRAP clear/>+<WRAP clear/> 
-[{{:c:ps1-1:2019:pasted:20190909-111117.png|skewness}}] +<WRAP column half> 
-<WRAP clear/>+<code> 
-[{{:c:ps1-1:2019:pasted:20190909-111001.png|modality}}] +set.seed(1) 
-<WRAP clear/>. +data <- rbeta(500, shape1 = 10, shape2 = 10) 
-box plot+hist(data, probability = TRUE,  
 +     main = "Histogram with Normal Distribution Data", 
 +     xlab = "Value", ylab = "Density",  
 +     col = "lightblue", border = "white"
 + 
 +# 2. 
 +# install.packages("fitdistrplus")  
 +library(fitdistrplus) 
 + 
 +fit <- fitdist(data, "beta"
 +alpha_est <- fit$estimate["shape1"
 +beta_est <- fit$estimate["shape2"
 + 
 +# 3. 
 +curve(dbeta(x, shape1 = alpha_est, shape2 = beta_est), 
 +      add = TRUE, col = "red", lwd = 2) 
 +</code> 
 +</WRAP> 
 + 
 +<WRAP column half> 
 +{{:b:head_first_statistics:pasted:20250903-074830.png}} 
 +</WRAP> 
 + 
 +<WRAP clear/> 
 +<WRAP column half> 
 +<code> 
 +## 
 +# right-skewed distribution 
 +1.  
 +set.seed(1) 
 +data <- rbeta(500, shape1 = 2, shape2 = 10) 
 +hist(data, probability = TRUE,  
 +     main = "Histogram with Right-skewed Distribution", 
 +     xlab = "Value", ylab = "Density",  
 +     col = "lightblue", border = "white"
 + 
 +# install.packages("fitdistrplus")  
 +library(fitdistrplus) 
 + 
 +fit <- fitdist(data, "beta"
 +alpha_est <- fit$estimate["shape1"
 +beta_est <- fit$estimate["shape2"
 + 
 +#  
 +curve(dbeta(x, shape1 = alpha_est, shape2 = beta_est), 
 +      add = TRUE, col = "red", lwd = 2) 
 +</code> 
 +</WRAP> 
 +<WRAP column half> 
 +{{:b:head_first_statistics:pasted:20250903-082513.png}} 
 +</WRAP> 
 +<WRAP clear/> 
 + 
 +====== Histogram Modality====== 
 +<WRAP column half> 
 +Unimodal  
 +<code> 
 +### unimodal data  
 +set.seed(1) 
 +d.1 <- rnorm(500, 10, 2) 
 +hist(d.1, breaks = 30, probability = T, 
 +     main = "Hist with Unimodal distrib", 
 +     xlab = "Value", ylab = "Density",  
 +     col = "lightblue", border = "black"
 +lines(density(d.1),  
 +      col = "darkred", lwd = 2) 
 +</code> 
 +</WRAP> 
 + 
 +<WRAP column half> 
 +{{:b:head_first_statistics:pasted:20250903-083409.png}} 
 +</WRAP> 
 + 
 +<WRAP clear/> 
 + 
 +Bimodal distribution 
 +<WRAP column half> 
 +<code> 
 +### bimodal data  
 +set.seed(1
 +d.1 <- rnorm(500, 10, 2) 
 +d.2 <- rnorm(500, 20, 2) 
 +d.all <- c(d.1, d.2) 
 +hist(d.all, breaks = 30, probability = T, 
 +     main = "Hist with bimodal distrib", 
 +     xlab = "Value", ylab = "Density",  
 +     col = "lightblue", border = "black"
 +lines(density(d.all),  
 +      col = "darkred", lwd = 2) 
 +</code> 
 +</WRAP> 
 + 
 +<WRAP column half> 
 +{{:b:head_first_statistics:pasted:20250903-083524.png}} 
 +</WRAP> 
 +<WRAP clear/> 
 + 
 +<WRAP column half> 
 +<code> 
 +### multi-modal data  
 +# Parameters for the first normal distribution (Mode 1) 
 +m.1 <- 50 
 +sd.1 <- 5 
 + 
 +# Parameters for the second normal distribution (Mode 2) 
 +m.2 <- 100 
 +sd.2 <- 15 
 + 
 +m.3 <- 160 
 +sd.3 <- 6 
 + 
 +# Mixing proportion for Mode 1 
 +prop.1 <- 0.3 
 +# Mixing proportion for Mode 2 
 +prop.2 <- 0.6 # This is 1 - prop1 
 +# Mixing proportion for Mode 2 
 +prop.3 <- 1.0 # This is 1 - prop1 
 + 
 +# Number of samples to generate 
 +n.sam <- 1000 
 + 
 +# Create an empty vector to store the combined samples 
 + 
 +mm.dist <- numeric(n.sam) 
 +set.seed(1) 
 +for (i in 1:n.sam) { 
 +  # Randomly choose which distribution to sample from 
 +  tmp <- runif(1) 
 +  if (tmp < prop.1) { 
 +    mm.dist[i] <- rnorm(1, mean = m.1, sd = sd.1) 
 +  } else if (tmp < prop.2) { 
 +    mm.dist[i] <- rnorm(1, mean = m.2, sd = sd.2) 
 +  } else { 
 +    mm.dist[i] <rnorm(1, mean = m.3, sd = sd.3) 
 +  } 
 + 
 +
 + 
 +hist(mm.dist, breaks = 30,  
 +     main = "Multimodal Distribution",  
 +     xlab = "Value", ylab = "Density",  
 +     freq = FALSE, probability = T, 
 +     col = "lightblue", border = "black"
 +lines(density(mm.dist),  
 +      col = "darkred", lwd = 2) 
 + 
 +</code> 
 +</WRAP> 
 +<WRAP column half> 
 +{{:b:head_first_statistics:pasted:20250908-082219.png}} 
 +</WRAP> 
 +<WRAP clear/> 
 + 
 + 
 +====== box plot ====== 
 +<WRAP column half>
 <code> <code>
 # Boxplot of MPG by Car Cylinders # Boxplot of MPG by Car Cylinders
Line 192: Line 388:
     ylab="Miles Per Gallon")     ylab="Miles Per Gallon")
 </code> </code>
-{{:c:ps1-1:2019:pasted:20190909-111438.png}}+</WRAP>
  
 +<WRAP column half>
 +{{:c:ps1-1:2019:pasted:20190909-111438.png}}
 +</WRAP>
 +<WRAP clear/>
 +====== see also ======
 +https://r-graph-gallery.com/
  
b/head_first_statistics/visualization.1725406582.txt.gz · Last modified: 2024/09/04 08:36 by hkimscil

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki