#membuat dataset
jam_belajar <- c(1,2,3,4,5)
skor_ujian <- c(50,55,65,70,80)

#model regresi linear
#lm=linear model
#lm(y ~ x)
#modeljb adalah nama model dari "model jam belajar"
modeljb <- lm(skor_ujian ~ jam_belajar)

#menampilkan ringkasan mode
summary(modeljb)
## 
## Call:
## lm(formula = skor_ujian ~ jam_belajar)
## 
## Residuals:
##    1    2    3    4    5 
##  1.0 -1.5  1.0 -1.5  1.0 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)   41.500      1.658   25.02 0.000140 ***
## jam_belajar    7.500      0.500   15.00 0.000643 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.581 on 3 degrees of freedom
## Multiple R-squared:  0.9868, Adjusted R-squared:  0.9825 
## F-statistic:   225 on 1 and 3 DF,  p-value: 0.0006431
#Menampilkan nilai  R-Squared, pakai $ kalau mau mengambil variabel/bagian tertentu
r_squared <- summary(modeljb)$r.squared
print(paste("Nilai R-Squared:", r_squared))
## [1] "Nilai R-Squared: 0.986842105263158"
#selang kepercayaan (confident interval)
confint(modeljb, level=0.95)
##                 2.5 %    97.5 %
## (Intercept) 36.222510 46.777490
## jam_belajar  5.908777  9.091223

Latihan 2

jam_lembur <- c(10,15,8,4,6)
gaji <- c(3200000,3400000,2900000,2700000,2850000)
model <- lm(gaji ~ jam_lembur)
summary(model)
## 
## Call:
## lm(formula = gaji ~ jam_lembur)
## 
## Residuals:
##      1      2      3      4      5 
##  98174 -29775 -70646  -8287  10534 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  2445927      80504  30.383 7.83e-05 ***
## jam_lembur     65590       8572   7.652  0.00464 ** 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 72330 on 3 degrees of freedom
## Multiple R-squared:  0.9513, Adjusted R-squared:  0.935 
## F-statistic: 58.55 on 1 and 3 DF,  p-value: 0.004636
r_squared <- summary(model)$r.squared
print(paste("Nilai R-Squared:", r_squared))
## [1] "Nilai R-Squared: 0.951257066089748"
confint(model, level=0.95)
##                 2.5 %     97.5 %
## (Intercept) 2189727.3 2702126.61
## jam_lembur    38309.9   92869.88

Latihan 3

data(cars)

#prompt "head" untuk menampilkan 6 data teratas, digunakan untuk melihat nama variabelnya
head(cars)
##   speed dist
## 1     4    2
## 2     4   10
## 3     7    4
## 4     7   22
## 5     8   16
## 6     9   10
modelcars <- lm(cars$dist ~ cars$speed)
#perlu dikasih "data=cars" supaya tersambung dengan dataset. Jika tidak, maka variabel "speed" dan "dist" tidak dapat terbaca

summary(modelcars)
## 
## Call:
## lm(formula = cars$dist ~ cars$speed)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -29.069  -9.525  -2.272   9.215  43.201 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) -17.5791     6.7584  -2.601   0.0123 *  
## cars$speed    3.9324     0.4155   9.464 1.49e-12 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 15.38 on 48 degrees of freedom
## Multiple R-squared:  0.6511, Adjusted R-squared:  0.6438 
## F-statistic: 89.57 on 1 and 48 DF,  p-value: 1.49e-12
r_squared <- summary(modelcars)$r.squared
print(paste("Nilai R-Squared:", r_squared))
## [1] "Nilai R-Squared: 0.651079380758251"
confint(modelcars, level=0.95)
##                  2.5 %    97.5 %
## (Intercept) -31.167850 -3.990340
## cars$speed    3.096964  4.767853
#plot(x,y), "main" untuk judul, "xlab" dab "ylab" untuk nama di garis x dan y, "pch (plot character" itu jenis titik, "col" itu warna, "abline" itu garis, "lwd" itu ketebalan garis
plot(cars$speed, cars$dist, main = "Regresi Linier: Speed vs Distance",
     xlab = "Kecepatan (speed)", ylab = "Jarak Berhenti (dist)", pch = 19, col = "blue")
abline(modelcars, col ="red", lwd = 2)

#uji korelasi
cor.test(cars$speed, cars$dist)
## 
##  Pearson's product-moment correlation
## 
## data:  cars$speed and cars$dist
## t = 9.464, df = 48, p-value = 1.49e-12
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  0.6816422 0.8862036
## sample estimates:
##       cor 
## 0.8068949