Descriptives

Mean

The mean of a random variable is given as

\[ \frac{1}{n}\Sigma_{i=1}^{n}x_{i} \]

data = c(10,22,53,46,54,62,71,38,69,10)

mean(data)
## [1] 43.5

Median

data = c(10,22,53,46,54,62,71,38,69,10)

median(data)
## [1] 49.5

Mode

data = c(10,22,53,46,54,62,71,38,69,10)

find_mode <- function(x) {
  u <- unique(x)
  tab <- tabulate(match(x, u))
  u[tab == max(tab)]
}

find_mode(data)
## [1] 10

Basic Graphs

Bar Chart

starwars %>% 
  select(eye_color) %>% 
  ggplot(aes(x = eye_color)) +
  geom_bar(fill = '#FA6713') +
  labs(y = 'Number of Individuals', x = 'Eye Color', title = 'Number of Individuals by Eye Color') +
  theme(axis.text.x = element_text(angle = 90))

Histogram

set.seed(1234)
d = rnorm(200, mean = 0, sd = 1)
hist(d, col = 'maroon', main='Histogram of Data', xlab = 'Data')

Box Plot

starwars %>% 
  drop_na %>% 
  select(sex, height) %>% 
  ggplot(aes(x = sex, y = height, fill = sex)) +
  geom_boxplot() +
  labs(x = 'Gender', y = 'Height', title = 'Heights of individuals by Gender', fill = 'Gender')

Pie Chart

Prop <- c(3,7,9,1,2)
 

myPalette <- brewer.pal(5, "Set2") 

# You can change the border of each area with the classical parameters:
pie(Prop , labels = c("A","B","C","D","E"), border="white", col=myPalette )

set.seed(2348907)
x <- rnorm(100)
y <- 2*(x+rnorm(100))

m = as.data.frame(cbind(x,y))

model1 = lm(y~x, data = m)
summary(model1)
## 
## Call:
## lm(formula = y ~ x, data = m)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -3.8412 -1.0767 -0.1808  1.2216  4.1540 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)   0.0454     0.1851   0.245    0.807    
## x             2.1087     0.1814  11.627   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.848 on 98 degrees of freedom
## Multiple R-squared:  0.5797, Adjusted R-squared:  0.5754 
## F-statistic: 135.2 on 1 and 98 DF,  p-value: < 2.2e-16



The regression model is given as
\[ Y = 0.0454 + 2.1087X \]

Line of best fit

ggplot()+geom_point(aes(x, y))+
  geom_smooth(aes(x,y),method = 'lm', se=F) +
  labs(title = 'Relationship between X and Y', x = 'X', y = 'Y')
## `geom_smooth()` using formula = 'y ~ x'

Random Sampling

# Getting some random values to use here
set.seed(seed = 14412)
thevalues <- sample(x = 1:100,size = 1000,replace = TRUE)
head(thevalues,20)
##  [1] 22 10 34 56 31  7 54 77 65 44 92 55 45 93 99 38 23 50 59 68
# Obtaining the unique vector of those values
thevalues.unique <- unique(thevalues)
thevalues.unique
##   [1]  22  10  34  56  31   7  54  77  65  44  92  55  45  93  99  38  23  50
##  [19]  59  68  16  96  87  47  19  15  43  60  82  49  88   6  40  98  53  13
##  [37]  11  18  72  71  66  76  81  61  42   8   3  39 100  57  79   5  32  17
##  [55]  27  94  26  67   1  85  36  58  97   4  70  35  95  12  80  30  75  90
##  [73]  84  46  91  83  21  37  14  86  51   9  78  74  69  64  41  25  89   2
##  [91]  63  73  20  48  52  24  29  28  62  33
# Create a sample without replacement (i.e. take the ball out and don't put it back in)
sample1 <- sample(x = thevalues.unique,size = 10,replace = FALSE)
sample1
##  [1]  16   5  43  18  46  70  83  56  85 100
# Remove the sampled items from the vector of values
thevalues.unique <- thevalues.unique[!(thevalues.unique %in% sample1)]
thevalues.unique
##  [1] 22 10 34 31  7 54 77 65 44 92 55 45 93 99 38 23 50 59 68 96 87 47 19 15 60
## [26] 82 49 88  6 40 98 53 13 11 72 71 66 76 81 61 42  8  3 39 57 79 32 17 27 94
## [51] 26 67  1 36 58 97  4 35 95 12 80 30 75 90 84 91 21 37 14 86 51  9 78 74 69
## [76] 64 41 25 89  2 63 73 20 48 52 24 29 28 62 33
# Another sample, and another removal
sample2 <- sample(x = thevalues.unique,size = 10,replace = FALSE)
thevalues.unique <- thevalues.unique[!(thevalues.unique %in% sample2)]
thevalues.unique
##  [1] 22 34 31  7 54 77 65 44 92 55 45 99 50 59 68 96 87 47 19 15 60 82 49 88  6
## [26] 40 98 53 13 11 72 71 66 76 81 61 42  3 39 57 79 32 17 26 67  1 36 58 97  4
## [51] 35 95 12 80 30 75 84 91 21 37 14 86 51  9 78 74 69 64 41 25 89  2 63 20 48
## [76] 52 24 29 28 62

Coin Toss Sample Space Generation

# toss a coin
coin <- c('heads', 'tails')

sample(coin, size = 1)
## [1] "tails"
# draw 2 elements without replacement
sample(coin, size = 2)
## [1] "tails" "heads"
sample(coin, size = 2, replace = FALSE)
## [1] "heads" "tails"
# draw 4 elements with replacement
sample(coin, size = 4, replace = TRUE)
## [1] "tails" "tails" "tails" "heads"
# five tosses
sample(coin, size = 5, replace = TRUE)
## [1] "heads" "tails" "heads" "heads" "tails"
# another five tosses
sample(coin, size = 5, replace = TRUE)
## [1] "tails" "heads" "heads" "tails" "tails"
# set random seed
# set.seed(1257)

# toss a coin with replacement
sample(coin, size = 4, replace = TRUE)
## [1] "tails" "heads" "tails" "heads"
# tossing a fair coin
coin <- c("heads", "tails")

sample(coin)
## [1] "heads" "tails"
# equivalent
sample(coin, prob = c(0.5, 0.5))
## [1] "tails" "heads"
# tossing a loaded coin (20% heads, 80% tails)
sample(coin, size = 5, replace = TRUE, prob = c(0.2, 0.8))
## [1] "tails" "tails" "tails" "tails" "heads"
# number of flips
num_flips <- 100

# flips simulation
coin <- c('heads', 'tails')
flips <- sample(coin, size = num_flips, replace = TRUE)

# number of heads and tails
freqs <- table(flips)
freqs
## flips
## heads tails 
##    56    44
# one more 100 flips
flips <- sample(coin, size = num_flips, replace = TRUE, prob=c(0.5, 0.5))
freqs <- table(flips)
freqs
## flips
## heads tails 
##    54    46
heads_freq <- cumsum(flips == 'heads') / 1:num_flips

plot(heads_freq,      # vector
     type = 'l',      # line type
     lwd = 2,         # width of line
     col = 'tomato',  # color of line
     las = 1,         # orientation of tick-mark labels
     ylim = c(0, 1),  # range of y-axis
     xlab = "number of tosses",    # x-axis label
     ylab = "relative frequency")  # y-axis label
abline(h = 0.5, col = 'gray50')