Rekapan UTS Pengantar Simulasi Statistika

Soal nomor 3

Data

x <- c(5, 8, 12, 13, 15, 18, 21)
set.seed(123)
n_boot <- 10000
medians <- numeric(n_boot)
for (i in 1:n_boot) {
  sample_x <- sample(x, size = length(x), replace = TRUE)
  medians[i] <- median(sample_x) }
ci <- quantile(medians, probs = c(0.025, 0.975))
print(ci)
##  2.5% 97.5% 
##     8    18

Soal nomor 4

Set seed

set.seed(123)
data <- rnorm(100, mean = 0, sd = 1)
filtered_data <- data[data > 1]
length(filtered_data)
## [1] 17

soal nomor 6

Set seed

set.seed(123)
x <- c(5, 7, 8, 10, 12)
n_boot <- 1000
means <- numeric(n_boot)
for (i in 1:n_boot) {
  sample_x <- sample(x, size = length(x), replace = TRUE)
  means[i] <- mean(sample_x)
  mean(means)
}

soal nomor 7

Set seed

set.seed(123)
data <- rnorm(100, mean = 70, sd = 5)
max(data)
## [1] 80.93666

Soal nomor 8

Set seed

set.seed(123)
data <- rbinom(100, size = 10, prob = 0.3)
mean(data)
## [1] 3.02

soal nomor 9

Set seed

set.seed(123)
x <- 1:10
y <- 2 * x + rnorm(10, 0, 1)
model <- lm(y ~ x)
coef(model)[2]
##        x 
## 1.918029

Soal nomor 10

# Set seed
set.seed(123)

data <- runif(100, min = 20, max = 80)

# Hitung range 
range_value <- diff(range(data))
range_value
## [1] 59.6187

Soal nomor 11

set.seed(123)

data <- rbinom(1000, size = 10, prob = 0.3)

mean(data)
## [1] 2.989

Soal nomor 13

set.seed(123)

data <- rnorm(100, mean = 50, sd = 10)

sd(data)
## [1] 9.128159

Nomor 14

set.seed(123)

data <- rexp(100, rate = 1)


median(data)
## [1] 0.847754

Nomor 15

set.seed(123)

data <- rnorm(100)

lower_bound <- -2
upper_bound <- 2

outliers <- sum(data < lower_bound | data > upper_bound)
outliers
## [1] 4

Nomor 16

# Set seed
set.seed(123)

data1 <- rnorm(50, mean = 100, sd = 15)

data2 <- rnorm(50, mean = 80, sd = 10)

data_gabungan <- c(data1, data2)

mean(data_gabungan)
## [1] 90.99007

Nomor 17

set.seed(123)

data <- rpois(100, lambda = 4)

modus <- as.numeric(names(sort(table(data), decreasing = TRUE)[1]))
modus
## [1] 2

Nomor 18

# Set seed
set.seed(42)

x1 <- rnorm(100)
x2 <- x1 + rnorm(100, 0, 0.01)
x3 <- rnorm(100)
y <- 3 + 2*x1 - 1*x3 + rnorm(100)

model <- lm(y ~ x1 + x2 + x3)

summary(model)
## 
## Call:
## lm(formula = y ~ x1 + x2 + x3)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -1.7944 -0.5867 -0.1038  0.6188  2.3280 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  3.03150    0.08914  34.007   <2e-16 ***
## x1           1.17483    9.89434   0.119    0.906    
## x2           0.88292    9.89031   0.089    0.929    
## x3          -1.03161    0.08882 -11.614   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.8867 on 96 degrees of freedom
## Multiple R-squared:  0.8927, Adjusted R-squared:  0.8893 
## F-statistic: 266.2 on 3 and 96 DF,  p-value: < 2.2e-16

Nomor 20

set.seed(123)

data <- sample(c("A", "B", "C"), size = 100, replace = TRUE, prob = c(0.2, 0.3, 0.5))

sum(data == "B")
## [1] 29