Distributions, descriptive statistics, probability

Goran Kardum

Department of Psychology

Probability

  • Probability refers to the likelihood of a particular event of interest occurring (Dancey, 2020).

  • Two similar, but not identical terms (Butler, 2021):

  • Relative frequency: The proportion of times some event occurs during a certain number of trials

  • n: Number of times a trial/experiment is run

Probability

  • N(A): The number of times that event A occurs during n trials

  • N(A)/n: Relative frequency of event A

  • Probability: The proportion of times an event occurs when the number of trials is very large

This is given by N(A)/n as n approaches infinity

Random and reproducible coin flips

  • Experiments with fair coin

  • In R language it is possible to use simulation - sample ()

  • Create vector with two possibilities

Experiment with coin in R

coin <- c('Heads', 'Tails')
  • sample(x, size, replace = FALSE, prob = NULL)

  • sample() has two mandatory arguments. The first argument, x, is the object from which a sample is to be drawn and the second argument, size, is the size of the sample to be drawn.

Sample function

sample (coin, 20, replace = TRUE)
 [1] "Heads" "Tails" "Heads" "Tails" "Tails" "Tails" "Heads" "Tails" "Tails"
[10] "Heads" "Tails" "Heads" "Tails" "Heads" "Tails" "Heads" "Tails" "Heads"
[19] "Heads" "Tails"

Repeating sampling

  • In real experiments or reproducible research we have situation that we must repeating sampling.

  • R have replicate() function to simulate sampling the same population many times

Replicate

replicate(7, sample(coin, 10, replace = TRUE))
      [,1]    [,2]    [,3]    [,4]    [,5]    [,6]    [,7]   
 [1,] "Tails" "Heads" "Heads" "Tails" "Tails" "Heads" "Heads"
 [2,] "Heads" "Tails" "Heads" "Tails" "Heads" "Heads" "Heads"
 [3,] "Tails" "Heads" "Heads" "Tails" "Heads" "Heads" "Heads"
 [4,] "Tails" "Tails" "Tails" "Heads" "Heads" "Tails" "Tails"
 [5,] "Tails" "Tails" "Heads" "Heads" "Heads" "Tails" "Tails"
 [6,] "Tails" "Tails" "Heads" "Tails" "Heads" "Heads" "Tails"
 [7,] "Tails" "Tails" "Tails" "Heads" "Heads" "Tails" "Heads"
 [8,] "Heads" "Heads" "Heads" "Tails" "Tails" "Heads" "Tails"
 [9,] "Heads" "Tails" "Tails" "Tails" "Heads" "Tails" "Heads"
[10,] "Heads" "Tails" "Heads" "Heads" "Tails" "Tails" "Heads"

Conditional probability

  • For the purpose of research in psychology it would be very important to understand conditional probability.

  • It is probability of some event which is dependent of some other event.

  • A conditional probability is the probability of a particular event happening if another event (or set of conditions) has also happened (Dancey, 2020).

Binomial distribution

  • A probability distribution is a mathematical distribution of scores where we know the probabilities associated with the occurrence of every score in the distribution. We know what the probability is of randomly selecting a particular score or set of scores from the distribution (Dancey, 2020).

  • The binomial distribution is a discrete probability distribution and describes the outcome of n independent trials in an experiment. Each trial is assumed to have only two outcomes, either success or failure.

  • Probability distribution that represents the probabilities of binomial random variables in binomial experiment

Example of binomial distribution

Let’s imagine an experiment with 10 identical six-sided dice. On one face of each there’s a letter A. Other dice face are empty. If we proceed to roll 10 dice what would be the probability to get 2 with letter A?

  • N denote the number of dice rolls in our experiment. In R that is parameter size

  • x a vector that specifying the outcomes whose probability we a trying to calculate

  • prob success probability for any one trial in the experiment

Functions of binomial distribution in R (Navarro, 2015)

  • The d form we’ve already seen: you specify a particular outcome x, and the output is the probability of obtaining exactly that outcome. (the “d” is short for density).

  • The p form calculates the cumulative probability. You specify a particular quantile q, and it tells you the probability of obtaining an outcome smaller than or equal to q.

  • The q form calculates the quantiles of the distribution. You specify a probability value p, and gives you the corresponding percentile. That is, the value of the variable for which there’s a probability p of obtaining an outcome lower than that value.

The r form is a random number generator: specifically, it generates n random outcomes from the distribution.

Example of binomial distribution

dbinom(x = 2, size = 10, prob = 1/6)
[1] 0.29071
  • We get a value of 0.29 and that is probability for that outcome

Binomial distribution - p

The probability of rolling 0, 1 and 2 letter A?

pbinom(q=2, size=10, prob = 1/6)
[1] 0.7752268
  • There is a 77.5% chance that we would roll 2 or fewer letter A.

  • The other interpretation: the value of 2 is the 77.5 percentile of this binomial distribution

Binomial distribution - q

If we want to calculate the 77th percentile of that binomial distribution — use qbinom() function

qbinom(p=0.77, size=10, prob=1/6)
[1] 2

Binomial distribution - r

If we repeat that 100 times… How? What happened with distribution?

  • use rbinom() function
rexamp <- rbinom(n=100, size=10, prob = 1/6)
rexamp
  [1] 1 2 2 2 3 2 2 1 3 3 1 1 4 2 3 1 2 4 4 1 1 2 1 4 2 4 3 2 1 1 2 4 3 3 4 2 3
 [38] 0 3 1 3 2 1 1 3 3 2 2 2 3 3 2 1 1 3 2 3 1 1 3 3 0 4 1 2 1 4 2 0 1 2 0 2 0
 [75] 3 2 2 3 2 1 1 2 1 1 3 0 2 2 3 2 0 0 2 3 1 1 4 2 0 5

Binomial distribution - r graph

hist(rexamp)

Example - binomial distribution

  • Suppose there are twenty multiple choice questions (class quiz). Each question has five possible answers, and only one of them is correct. Find the probability of having five or less correct answers if a student attempts to answer every question at random.
  • Exactly 5 correct answers by random attempts?
  • Five or less questions (cumulative probability)

  • Exactly 5 correct answers by random attempts…
  • Why prob=0.2… because each question have 5 possibility (1/5)
dbinom(5, size=20, prob=0.2)
[1] 0.1745595

  • five or less
pbinom(5, size=20, prob=0.2)
[1] 0.8042078

Normal distribution

  • Normal distribution

  • The bell curve distribution

  • Gaussian distribution

  • Describing with two parameters: the mean of the distribution μ and the standard deviation of the distribution σ

  • in R we have also functions: dnorm(), pnorm(), qnorm(), rnorm()

Generate normal distribution in R

library(ggplot2)
ggplot(data.frame(x = c(-4, 4)), aes(x = x)) +
stat_function(fun = dnorm)

Normal curve (percent of corresponding area)

https://rpubs.com/CarstenGrube/573181

Normal curve - rnorm() function

hist(rnorm(100, mean = 100, sd = 10))

Normal curve - pnorm() function

pnorm(110,mean=100,sd=10)
[1] 0.8413447
  • pnorm() calculate cumulative probabilities. In that example pnorm() calculate value less or equal to 110.

Normal curve - pnorm() function

  • it is also possible to calculate the interval value:
pnorm(110, mean=100, sd=10) - pnorm(120, mean=100, sd=10)
[1] -0.1359051

Normal curve - qnorm() funcion

  • calculate percentiles for exact value
qnorm(0.80, mean = 100, sd=10)
[1] 108.4162

Platykurtic and leptokurtic distribution

Comparison of means

Skewness (Dancey, 2020)

  • Skewed distributions are those where the peak is shifted away from the centre of the distribution and there is an extended tail on one of the sides of the peak.

  • A negatively skewed distribution is one where the peak has been shifted to the right towards the high numbers on the scale and the tail is pointing to the low number (or even pointing to the negative numbers).

  • A positively skewed distribution has the peak shifted left, towards the low numbers, and has the tailed extended towards the high numbers.

Skewness

  • Skewness value lower than -1 or higher than +1: distribution is assimetric

  • Skewness value between -0.5 and -1 as +0.5 and +1 - assimetric but not to much

  • Skewness value between -0.5 and 0.5 - we could conclude that is simmetric distribution

Skewness - example

library(psych)
v_x <- c(1,2,3,4,5,6,7,8,9,10)
describe(v_x)
   vars  n mean   sd median trimmed  mad min max range skew kurtosis   se
X1    1 10  5.5 3.03    5.5     5.5 3.71   1  10     9    0    -1.56 0.96
v_y <- c(1,2,3,4,5,6,7,8,9,15)
describe(v_y)
   vars  n mean   sd median trimmed  mad min max range skew kurtosis   se
X1    1 10    6 4.08    5.5     5.5 3.71   1  15    14 0.79    -0.25 1.29
v_z <- c(1,5,6,7,8,9,10,11,12,13)
describe(v_z)
   vars  n mean   sd median trimmed  mad min max range  skew kurtosis   se
X1    1 10  8.2 3.61    8.5     8.5 3.71   1  13    12 -0.48    -0.87 1.14

Kurtosis

  • Kurtosis is a measure of the peakedness or how heavy the tails of a distribution.

  • The kurtosis of a normal distribution is 3.

  • If a given distribution has a kurtosis less than 3, it is said to be playkurtic, which means it tends to produce fewer and less extreme outliers than the normal distribution.

  • If a given distribution has a kurtosis greater than 3, it is said to be leptokurtic, which means it tends to produce more outliers than the normal distribution.

Q-Q plot

  • The qq plot is used to assess the normality of a distribution of sample data.

  • The better the data points fit the qq line, the more normal the data is.

  • QQ plots tend to make it easier to find outliers

qq_ex <- rnorm(100, mean = 100, sd = 1)
qqnorm(qq_ex, pch = 1, frame=FALSE)
qqline(qq_ex, col="steelblue", lwd=2)

Poisson distribution

  • The Poisson distribution is used in situations when we observe the counts of events within a set unit of time, area, volume, length…

  • The Poisson distribution is a discrete probability distribution for the counts of events that occur randomly in a given interval of time (or space).

  • Binomial and Poisson distributions - probability distributions were characterized by a formula for the probability of each possible discrete value.

Poisson distribution - example

Given that 5% of a population are left-handed, use the Poisson distribution to estimate the probability that a random sample of 100 people contains 2 or more left-handed people

Example

Create variable with some data… analysis with psych package (Revelle, 2022)

data <- c(5.56, 3.45, 4.55, 6.2, 4.8, 6.8, 7.2, 8.3, 3.2, 7.7, 6.9, 4.1, 8.3, 7.5, 7.7, 4.3, 3.8, 2.8, 3.2, 8.9)
mean(data)
[1] 5.763
median(data)
[1] 5.88
library(psych)
datas_1<- rnorm(20, mean = 5.7, sd = 2)
SD(data)
[1] 2.004275
median(datas_1)
[1] 5.646525

Literature

Dancey, C. (2020). Statistics Without Maths For Psychology (8th ed.). Pearson Education Limited.
Navarro, D. (2015). Learning Statistics with R: A tutorial for psychology students and other beginners. University of Adelaide. https://health.adelaide.edu.au/psychology/ccs/teaching/lsr/
Revelle, W. (2022). Psych: Procedures for psychological, psychometric, and personality research. Northwestern University. https://CRAN.R-project.org/package=psych