library(csvread)
library(car)
## Loading required package: carData
library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5     v purrr   0.3.4
## v tibble  3.1.6     v dplyr   1.0.7
## v tidyr   1.1.4     v stringr 1.4.0
## v readr   2.1.0     v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
## x dplyr::recode() masks car::recode()
## x purrr::some()   masks car::some()
data <- read.csv("D:/STATISTIKA IPB/SEMESTER 4/ANALISIS REGRESI/Student_Marks.csv")
data <- data[,-1]
colnames(data) <- c("Time Study", "Marks")
names(data)
## [1] "Time Study" "Marks"
#Model Regresi
model.regresi <- lm(Marks~`Time Study`, data=data)
summary(model.regresi)
## 
## Call:
## lm(formula = Marks ~ `Time Study`, data = data)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
## -7.866 -4.034 -0.384  2.979 10.628 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept)    1.2239     0.9623   1.272    0.206    
## `Time Study`   5.6888     0.2042  27.853   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 4.822 on 98 degrees of freedom
## Multiple R-squared:  0.8878, Adjusted R-squared:  0.8867 
## F-statistic: 775.8 on 1 and 98 DF,  p-value: < 2.2e-16
#Anova 
anova(model.regresi)
## Analysis of Variance Table
## 
## Response: Marks
##              Df  Sum Sq Mean Sq F value    Pr(>F)    
## `Time Study`  1 18039.9 18039.9  775.77 < 2.2e-16 ***
## Residuals    98  2278.9    23.3                      
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#Selang Kepercayaan Parameter Regresi
#B0
confint(model.regresi, "(Intercept)", 0.95)
##                  2.5 %   97.5 %
## (Intercept) -0.6857583 3.133475
#B1
confint(model.regresi, "`Time Study`", 0.95)
##                 2.5 %   97.5 %
## `Time Study` 5.283434 6.094067
plot(data$`Time Study`, data$Marks)
abline(model.regresi, col="blue", lwd=2)

cor(data$`Time Study`, data$Marks)
## [1] 0.9422539
#Ukuran Keragaman
jkg <- sum((data$`Time Study` - model.regresi$fitted.values)^2)
jkr <- sum((model.regresi$fitted.values - mean(data$`Time Study`))^2)
jkt <- sum((data$`Time Study` - mean(data$`Time Study`))^2)
jkt2 <- jkr+jkg
#Uji hipotesis
alpha = 0.05
n = dim(data)[1]
#Uji t 
#H0 : beta_1 = 0
summary(model.regresi) 
## 
## Call:
## lm(formula = Marks ~ `Time Study`, data = data)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
## -7.866 -4.034 -0.384  2.979 10.628 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept)    1.2239     0.9623   1.272    0.206    
## `Time Study`   5.6888     0.2042  27.853   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 4.822 on 98 degrees of freedom
## Multiple R-squared:  0.8878, Adjusted R-squared:  0.8867 
## F-statistic: 775.8 on 1 and 98 DF,  p-value: < 2.2e-16
t_crit <- abs(qt(alpha/2, n-2))
t_crit
## [1] 1.984467
colnames(data) <- c("x","y")
model.regresi <- lm(y ~ x, data=data)
#1. Ukuran Kebaikan Model (R-squared)
#melihat dari summary model
summary(model.regresi)
## 
## Call:
## lm(formula = y ~ x, data = data)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
## -7.866 -4.034 -0.384  2.979 10.628 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)   1.2239     0.9623   1.272    0.206    
## x             5.6888     0.2042  27.853   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 4.822 on 98 degrees of freedom
## Multiple R-squared:  0.8878, Adjusted R-squared:  0.8867 
## F-statistic: 775.8 on 1 and 98 DF,  p-value: < 2.2e-16
#2. Prediksi Nilai Y dan SK
#memasukkan x tertentu
newdata <- data.frame(x=6)
#SK bagi individu
predict(model.regresi, newdata, interval = "prediction")
##        fit     lwr      upr
## 1 35.35636 25.7075 45.00522
#SK bagi rataan y
predict(model.regresi, newdata, interval = "confidence")
##        fit      lwr      upr
## 1 35.35636 34.12219 36.59053