Skip to content

Instantly share code, notes, and snippets.

View NyaGarcia's full-sized avatar
🐈

Nya NyaGarcia

🐈
View GitHub Profile
@NyaGarcia
NyaGarcia / gears.r
Created March 18, 2020 15:42
Showing the number of gears for each car in the dataset
> mtcars$gear
[1] 4 4 4 3 3 3 3 4 4 4 4 3 3 3 3 3 3 4 4 4 3 3 3 3 3 4 5 5 5 5 5 4
@NyaGarcia
NyaGarcia / contingency.r
Created March 18, 2020 15:46
Creating a contingency table of the cyl and gear columns
table(mtcars$gear, mtcars$cyl)
4 6 8
3 1 2 12
4 8 4 0
5 2 1 2
@NyaGarcia
NyaGarcia / prop-table.r
Created March 18, 2020 15:50
Creating a contingency table with percentages rather than values
prop.table(table(mtcars$gear, mtcars$cyl))
4 6 8
3 0.03125 0.06250 0.37500
4 0.25000 0.12500 0.00000
5 0.06250 0.03125 0.06250
@NyaGarcia
NyaGarcia / chisquare.r
Created March 18, 2020 16:07
Performing the pearson chi-square test for independence between gears and cylinders
> chisq.test(mtcars$gear, mtcars$cyl)
Pearson's Chi-squared test
data: mtcars$gear and mtcars$cyl
X-squared = 18.036, df = 4, p-value = 0.001214
Warning message:
In chisq.test(mtcars$gear, mtcars$cyl) :
Chi-squared approximation may be incorrect
@NyaGarcia
NyaGarcia / fisher-test.r
Created March 18, 2020 16:53
Performing the Fisher exact test for independence between gears and cylinders
> fisher.test(mtcars$gear, mtcars$cyl)
Fisher's Exact Test for Count Data
data: mtcars$gear and mtcars$cyl
p-value = 8.26e-05
alternative hypothesis: two.sided
@NyaGarcia
NyaGarcia / montecarlo.r
Created March 18, 2020 18:26
Performing the pearson chi-square test for independence between gears and cylinders with Monte Carlo simulation
> chisq.test(mtcars$gear, mtcars$cyl, simulate.p.value = TRUE)
Pearson's Chi-squared test with simulated p-value (based on 2000
replicates)
data: mtcars$gear and mtcars$cyl
X-squared = 18.036, df = NA, p-value = 0.0004998
@NyaGarcia
NyaGarcia / percentages.r
Created March 18, 2020 19:05
Percentages in contingency table
> prop.table(table(mtcars$gear, mtcars$cyl))*100
4 6 8
3 3.125 6.250 37.500
4 25.000 12.500 0.000
5 6.250 3.125 6.250
@NyaGarcia
NyaGarcia / gtest.r
Last active March 19, 2020 15:24
Performing the Gtest for independence between gears and cylinders
> library(DescTools)
> GTest(mtcars$gear, mtcars$cyl)
Log likelihood ratio (G-test) test of independence without
correction
data: mtcars$gear and mtcars$cyl
G = 23.26, X-squared df = 4, p-value = 0.0001123
@NyaGarcia
NyaGarcia / addmargins.r
Created March 18, 2020 19:20
Calculating total values for rows and columns with addmargins()
> addmargins(table(mtcars$gear, mtcars$cyl))
4 6 8 Sum
3 1 2 12 15
4 8 4 0 12
5 2 1 2 5
Sum 11 7 14 32
@NyaGarcia
NyaGarcia / colSums.r
Created March 18, 2020 19:24
Calculating total values for columns with colSums()
> colSums(table(mtcars$gear, mtcars$cyl))
4 6 8
11 7 14