1

n <- 10000
u <- runif(n)
set.seed(10)
λ <- seq(from = 0, to = 1)
z <- -(log(1-u)/λ)
head(z)
## [1]       Inf 1.9543690       Inf 0.5578626       Inf 2.5915683
length(z)
## [1] 10000
hist(z,main="Exp dari Inverse Transform")

library(moments)
kurtosis(z)
## [1] NaN
skewness(z)
## [1] NaN

2a

z = read.csv("C:/Users/User/Downloads/StudentsPerformance.csv",header=TRUE)
z$total.score = rowSums(z[6:8])
z$average.score = rowMeans(z[6:8])
head(z)
##   gender race.ethnicity parental.level.of.education        lunch
## 1 female        group B           bachelor's degree     standard
## 2 female        group C                some college     standard
## 3 female        group B             master's degree     standard
## 4   male        group A          associate's degree free/reduced
## 5   male        group C                some college     standard
## 6 female        group B          associate's degree     standard
##   test.preparation.course math.score reading.score writing.score total.score
## 1                    none         72            72            74         218
## 2               completed         69            90            88         247
## 3                    none         90            95            93         278
## 4                    none         47            57            44         148
## 5                    none         76            78            75         229
## 6                    none         71            83            78         232
##   average.score
## 1      72.66667
## 2      82.33333
## 3      92.66667
## 4      49.33333
## 5      76.33333
## 6      77.33333

2b

library(moments)
summary(z)
##     gender          race.ethnicity     parental.level.of.education
##  Length:1000        Length:1000        Length:1000                
##  Class :character   Class :character   Class :character           
##  Mode  :character   Mode  :character   Mode  :character           
##                                                                   
##                                                                   
##                                                                   
##     lunch           test.preparation.course   math.score     reading.score   
##  Length:1000        Length:1000             Min.   :  0.00   Min.   : 17.00  
##  Class :character   Class :character        1st Qu.: 57.00   1st Qu.: 59.00  
##  Mode  :character   Mode  :character        Median : 66.00   Median : 70.00  
##                                             Mean   : 66.09   Mean   : 69.17  
##                                             3rd Qu.: 77.00   3rd Qu.: 79.00  
##                                             Max.   :100.00   Max.   :100.00  
##  writing.score     total.score    average.score   
##  Min.   : 10.00   Min.   : 27.0   Min.   :  9.00  
##  1st Qu.: 57.75   1st Qu.:175.0   1st Qu.: 58.33  
##  Median : 69.00   Median :205.0   Median : 68.33  
##  Mean   : 68.05   Mean   :203.3   Mean   : 67.77  
##  3rd Qu.: 79.00   3rd Qu.:233.0   3rd Qu.: 77.67  
##  Max.   :100.00   Max.   :300.0   Max.   :100.00
skewness(z[6:10])
##    math.score reading.score writing.score   total.score average.score 
##    -0.2785166    -0.2587157    -0.2890096    -0.2986083    -0.2986083
kurtosis(z[6:10])
##    math.score reading.score writing.score   total.score average.score 
##      3.267597      2.926081      2.960808      3.119221      3.119221

2c

a = table(z$gender)
A = round(100*a/sum(a),1)
pie(a, labels = A, main = 'Diagram Lingkaran Gender',col = rainbow(length(a)))
legend('topright', c('Male', 'Female'), cex = 0.8, fill = rainbow(length(a)))

b = table(z$race.ethnicity)
B = round(100*b/sum(b),1)
pie(b, labels = B, main = 'Diagram Lingkaran Race ethnicity',col = rainbow(length(b)))
legend('topright', c('Group A', 'Group B', 'Group C', 'Group D', 'Group E'), cex = 0.8, fill = rainbow(length(b)))

c = table(z$parental.level.of.education)
C = round(100*c/sum(c),1)
pie(c, labels = C, main = 'Pie Chart of Parental Level of Education',col = rainbow(length(c)))
legend('topright', c('High School', 'Some High School', 'Some College', "Associate's Degree", "Bachelor's Degree", "Master's Degree"), cex = 0.8, fill = rainbow(length(c)))

d = table(z$lunch)
D = round(100*d/sum(d),1)
pie(d, labels = D, main = 'Pie Chart of Lunch',col = rainbow(length(d)))
legend('topright', c('Standard', 'Free/Reduced'), cex = 0.8, fill = rainbow(length(d)))

e = table(z$test.preparation.course)
E = round(100*e/sum(e),1)
pie(e, labels = E, main = 'Pie Chart of Test Preparation Course',col = rainbow(length(e)))
legend('topright', c('None', 'Completed'), cex = 0.8, fill = rainbow(length(e)))

2d

library(ggplot2)

print(rataan1 <- aggregate(total.score ~ parental.level.of.education, data = z, FUN = mean))
##   parental.level.of.education total.score
## 1          associate's degree    208.7072
## 2           bachelor's degree    215.7712
## 3                 high school    189.2908
## 4             master's degree    220.7966
## 5                some college    205.4292
## 6            some high school    195.3240
print(barchart <- ggplot(rataan1, aes(x = reorder(rataan1$parental.level.of.education, -rataan1$total.score), y = rataan1$total.score)) + geom_bar(stat = 'identity', color = 'black', fill = 'burly wood') + theme(axis.text.x = element_text(angle = 45, hjust = 1)) + labs(title = 'Average Score', subtitle = 'Average Score of Parental Level of Education', y = 'Average Score', x = 'Parental Level of Education'))
## Warning: Use of `rataan1$parental.level.of.education` is discouraged. Use
## `parental.level.of.education` instead.
## Warning: Use of `rataan1$total.score` is discouraged. Use `total.score` instead.
## Use of `rataan1$total.score` is discouraged. Use `total.score` instead.

print(rataan2 <- aggregate(total.score ~ race.ethnicity, data = z, FUN = mean))
##   race.ethnicity total.score
## 1        group A    188.9775
## 2        group B    196.4053
## 3        group C    201.3950
## 4        group D    207.5382
## 5        group E    218.2571
print(barchart2 <- ggplot(rataan2, aes(x = reorder(rataan2$race.ethnicity, -rataan2$total.score), y = rataan2$total.score)) + geom_bar(stat = 'identity', color = 'black', fill = 'cadet blue') + theme(axis.text.x = element_text(angle = 45, hjust = 1)) + labs(title = 'Average Score', subtitle = 'Average Score of Race Ethnicity', y = 'Average Score', x = 'Race Ethnicity'))
## Warning: Use of `rataan2$race.ethnicity` is discouraged. Use `race.ethnicity`
## instead.
## Warning: Use of `rataan2$total.score` is discouraged. Use `total.score` instead.
## Use of `rataan2$total.score` is discouraged. Use `total.score` instead.

2e

library(ggplot2)
ggplot(z, aes(fill = gender, y = total.score, x = parental.level.of.education)) + geom_bar(position = 'stack', stat = 'identity') + ggtitle('Parental Level of Education and Gender of Total Score') + xlab('')

ggplot(z, aes(fill = gender, y = total.score, x = race.ethnicity)) + geom_bar(position = 'stack', stat = 'identity') + ggtitle('Race Ethnicity and Gender of Total Score') + xlab('')

2f

hist(z$math.score [z$gender == 'male'], main = 'Histogram Math Score (Male)', col = 'chocolate', xlab = 'Math Score (Male)', ylab = 'Frequency', border = 'black')

hist(z$math.score [z$gender == 'female'], main = 'Histogram Math Score (Female)', col = 'dark khaki', xlab = 'Math Score (Female)', ylab = 'Frequency', border = 'black')

hist(z$reading.score [z$gender == 'male'], main = 'Histogram Reading Score (Male)', col = 'dark seagreen', xlab = 'Reading Score (Male)', ylab = 'Frequency', border = 'black')

hist(z$reading.score [z$gender == 'female'], main = 'Histogram Reading Score (Female)', col = 'light blue', xlab = 'Reading Score (Female)', ylab = 'Frequency', border = 'black')

hist(z$writing.score [z$gender == 'male'], main = 'Histogram Writing Score (Male)', col = 'dim gray', xlab = 'Writing Score (Male)', ylab = 'Frequency', border = 'black')

hist(z$writing.score [z$gender == 'female'], main = 'Histogram Writing Score (Female)', col = 'maroon', xlab = 'Writing Score (Female)', ylab = 'Frequency', border = 'black')

2g

boxplot(z$math.score [z$gender == 'male'] ~ z$parental.level.of.education [z$gender == 'male'], data = z, main = 'Boxplot of Math Score on Parental Education Level (Male)', xlab = 'Parental Level of Education (Male)', ylab = 'Math Score (Male)', col = 'indian red', border = 'black')

boxplot(z$math.score [z$gender == 'female'] ~ z$parental.level.of.education [z$gender == 'female'], data = z, main = 'Boxplot Math Score Parental Education Level (Female)', xlab = 'Parental Level of Education (Female)', ylab = 'Math Score (Female)', col = 'lavender', border = 'black')

boxplot(z$reading.score [z$gender == 'male'] ~ z$parental.level.of.education [z$gender == 'male'], data = z, main = 'Boxplot of Reading Score on Parental Education Level (Male)', xlab = 'Parental Level of Education (Male)', ylab = 'Reading Score (Male)', col = 'light green', border = 'black')

boxplot(z$reading.score [z$gender == 'female'] ~ z$parental.level.of.education [z$gender == 'female'], data = z, main = 'Boxplot of Reading Score on Parental Education Level (Female)', xlab = 'Parental Level of Education (Female)', ylab = 'Reading Score (Female)', col = 'sky blue', border = 'black')

boxplot(z$writing.score [z$gender == 'male'] ~ z$parental.level.of.education [z$gender == 'male'], data = z, main = 'Boxplot of Writing Score on Parental Education Level (Male)', xlab = 'Parental Level of Education (Male)', ylab = 'Writing Score (Male)', col = 'medium purple', border = 'black')

boxplot(z$writing.score [z$gender == 'female'] ~ z$parental.level.of.education [z$gender == 'female'], data = z, main = 'Boxplot of Writing Score on Parental Education Level (Female)', xlab = 'Parental Level of Education (Female)', ylab = 'Writing Score (Female)', col = 'light steel blue', border = 'black')

2h

plot(z$math.score, z$reading.score, xlab = 'Math Score', ylab = 'Reading Score', main = 'Scatterplot of Math and Reading Score', col = 'rosy brown')

plot(z$math.score, z$writing.score, xlab = 'Math Score', ylab = 'Writing Score', main = 'Scatterplot of Math and Writing Score', col = 'slate gray')

plot(z$reading.score, z$writing.score, xlab = 'Reading Score', ylab = 'Writing Score', main = 'Scatterplot of Reading and Writing Score', col = 'yellow green')