data=read.table(file.choose(),header = TRUE)
data
##      Y  X1   X2   X3
## 1 57.5  78 2.75 29.5
## 2 52.8  69 2.15 26.3
## 3 61.3  77 4.41 32.2
## 4 67.0  88 5.52 36.5
## 5 53.5  67 3.21 27.2
## 6 62.7  80 4.32 27.7
## 7 56.2  74 2.31 28.3
## 8 68.5  94 4.30 30.3
## 9 69.2 102 3.71 28.7
#membuat scatter plot antara variabel X1 dan Y
library(ggplot2)
ggplot(data, aes(x = X1, y = Y)) +
  geom_point(color = "blue", size = 1) +
  geom_smooth(
    method = "lm",
    color = "red",
    fill = "pink",
    se = TRUE
  ) +
  labs(
    title = "Scatter Plot X1 vs Y",
    x = "X1",
    y = "Y"
  ) +
  theme_minimal()
## `geom_smooth()` using formula = 'y ~ x'

#membuat scatter plot antara variabel X2 dan Y
library(ggplot2)
ggplot(data, aes(x = X2, y = Y)) +
  geom_point(color = "blue", size = 1) +
  geom_smooth(
    method = "lm",
    color = "red",
    fill = "pink",
    se = TRUE
  ) +
  labs(
    title = "Scatter Plot X2 vs Y",
    x = "X2",
    y = "Y"
  ) +
  theme_minimal()
## `geom_smooth()` using formula = 'y ~ x'

#membuat scatter plot antara variabel X3 dan Y
library(ggplot2)
ggplot(data, aes(x = X3, y = Y)) +
  geom_point(color = "blue", size = 1) +
  geom_smooth(
    method = "lm",
    color = "red",
    fill = "pink",
    se = TRUE
  ) +
  labs(
    title = "Scatter Plot X3 vs Y",
    x = "X3",
    y = "Y"
  ) +
  theme_minimal()
## `geom_smooth()` using formula = 'y ~ x'

#ANALISIS RLS X1 DAN Y
library(lmtest)
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
model1=lm(formula=Y~X1,data = data)
summary(model1)
## 
## Call:
## lm(formula = Y ~ X1, data = data)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -2.6440 -1.9128 -0.2151  2.2513  2.4075 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 19.01108    5.42272   3.506 0.009915 ** 
## X1           0.51797    0.06635   7.807 0.000106 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 2.174 on 7 degrees of freedom
## Multiple R-squared:  0.897,  Adjusted R-squared:  0.8823 
## F-statistic: 60.95 on 1 and 7 DF,  p-value: 0.0001065
#PENGUJIAN ASUMSI#
library(stats)
library(car)
## Loading required package: carData
library(nortest)
#Uji Normalitas
shapiro.test(resid(model1))
## 
##  Shapiro-Wilk normality test
## 
## data:  resid(model1)
## W = 0.87986, p-value = 0.1565
#Uji Heteroskedastisidas
bptest(model1,varformula = ~fitted.values(model1),studentize = F)
## 
##  Breusch-Pagan test
## 
## data:  model1
## BP = 0.35178, df = 1, p-value = 0.5531
#Uji Autokorelasi
dwtest(model1,alternative = 'two.sided')
## 
##  Durbin-Watson test
## 
## data:  model1
## DW = 1.785, p-value = 0.5744
## alternative hypothesis: true autocorrelation is not 0
bgtest(model1)
## 
##  Breusch-Godfrey test for serial correlation of order up to 1
## 
## data:  model1
## LM test = 0.033549, df = 1, p-value = 0.8547
#UJI ASUMSI HETEROSKEDASITAS MENGGUNAKAN UJI GLEJSER (MODEL 1)
resid(model1)
##          1          2          3          4          5          6          7 
## -1.9127561 -1.9510242  2.4052142  2.4075419 -0.2150838  2.2513035 -1.1408752 
##          8          9 
##  0.7997207 -2.6440410
residual=resid(model1)
abs(residual)
##         1         2         3         4         5         6         7         8 
## 1.9127561 1.9510242 2.4052142 2.4075419 0.2150838 2.2513035 1.1408752 0.7997207 
##         9 
## 2.6440410
residual1=abs(residual)
model1new=lm(formula=residual1~X1,data = data)
summary(model1new)
## 
## Call:
## lm(formula = residual1 ~ X1, data = data)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -1.3200 -0.4062  0.2952  0.5324  0.7722 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)
## (Intercept) -0.57193    2.04979  -0.279    0.788
## X1           0.02864    0.02508   1.142    0.291
## 
## Residual standard error: 0.8219 on 7 degrees of freedom
## Multiple R-squared:  0.157,  Adjusted R-squared:  0.03657 
## F-statistic: 1.304 on 1 and 7 DF,  p-value: 0.2911
#ANALISIS RLS X2 DAN Y
library(lmtest)
model2=lm(formula=Y~X2,data = data)
summary(model2)
## 
## Call:
## lm(formula = Y ~ X2, data = data)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -5.6496 -2.1172 -1.2392  0.9339  7.8929 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)   45.298      5.254   8.621 5.64e-05 ***
## X2             4.315      1.390   3.105   0.0172 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 4.394 on 7 degrees of freedom
## Multiple R-squared:  0.5793, Adjusted R-squared:  0.5192 
## F-statistic:  9.64 on 1 and 7 DF,  p-value: 0.0172
#PENGUJIAN ASUMSI#
library(stats)
library(car)
library(nortest)
#Uji Normalitas
shapiro.test(resid(model2))
## 
##  Shapiro-Wilk normality test
## 
## data:  resid(model2)
## W = 0.93927, p-value = 0.5742
#Uji Heteroskedastisidas
bptest(model2,varformula = ~fitted.values(model2),studentize = F)
## 
##  Breusch-Pagan test
## 
## data:  model2
## BP = 0.061595, df = 1, p-value = 0.804
#Uji Autokorelasi
dwtest(model2,alternative = 'two.sided')
## 
##  Durbin-Watson test
## 
## data:  model2
## DW = 0.50193, p-value = 0.005426
## alternative hypothesis: true autocorrelation is not 0
bgtest(model2)
## 
##  Breusch-Godfrey test for serial correlation of order up to 1
## 
## data:  model2
## LM test = 5.4817, df = 1, p-value = 0.01922
#UJI ASUMSI HETEROSKEDASITAS MENGGUNAKAN UJI GLEJSER (MODEL 2)
resid(model2)
##          1          2          3          4          5          6          7 
##  0.3353100 -1.7757020 -3.0275565 -2.1171842 -5.6495807 -1.2392084  0.9339012 
##          8          9 
##  4.6470912  7.8929294
residual=resid(model2)
abs(residual)
##         1         2         3         4         5         6         7         8 
## 0.3353100 1.7757020 3.0275565 2.1171842 5.6495807 1.2392084 0.9339012 4.6470912 
##         9 
## 7.8929294
residual2=abs(residual)
model2new=lm(formula=residual2~X2,data = data)
summary(model2new)
## 
## Call:
## lm(formula = residual2 ~ X2, data = data)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -2.3452 -1.7837 -0.6405  1.2837  4.7895 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)
## (Intercept)   1.4690     3.1460   0.467    0.655
## X2            0.4406     0.8321   0.529    0.613
## 
## Residual standard error: 2.631 on 7 degrees of freedom
## Multiple R-squared:  0.03851,    Adjusted R-squared:  -0.09885 
## F-statistic: 0.2804 on 1 and 7 DF,  p-value: 0.6128
#ANALISIS RLS X3 DAN Y
library(lmtest)
model3=lm(formula=Y~X3,data = data)
summary(model3)
## 
## Call:
## lm(formula = Y ~ X3, data = data)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
## -4.693 -3.315 -2.592  3.937  9.297 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)
## (Intercept)  27.1873    18.9650   1.434    0.195
## X3            1.1399     0.6369   1.790    0.117
## 
## Residual standard error: 5.611 on 7 degrees of freedom
## Multiple R-squared:  0.314,  Adjusted R-squared:  0.216 
## F-statistic: 3.204 on 1 and 7 DF,  p-value: 0.1166
#PENGUJIAN ASUMSI#
library(stats)
library(car)
library(nortest)
#Uji Normalitas
shapiro.test(resid(model3))
## 
##  Shapiro-Wilk normality test
## 
## data:  resid(model3)
## W = 0.81761, p-value = 0.03238
#Uji Heteroskedastisidas
bptest(model3,varformula = ~fitted.values(model3),studentize = F)
## 
##  Breusch-Pagan test
## 
## data:  model3
## BP = 0.32297, df = 1, p-value = 0.5698
#Uji Autokorelasi
dwtest(model3,alternative = 'two.sided')
## 
##  Durbin-Watson test
## 
## data:  model3
## DW = 1.117, p-value = 0.1651
## alternative hypothesis: true autocorrelation is not 0
bgtest(model3)
## 
##  Breusch-Godfrey test for serial correlation of order up to 1
## 
## data:  model3
## LM test = 0.80317, df = 1, p-value = 0.3701
#UJI ASUMSI HETEROSKEDASITAS MENGGUNAKAN UJI GLEJSER (MODEL 3)
resid(model3)
##         1         2         3         4         5         6         7         8 
## -3.314678 -4.366959 -2.592442 -1.794065 -4.692880  3.937164 -3.246783  6.773392 
##         9 
##  9.297252
residual=resid(model3)
abs(residual)
##        1        2        3        4        5        6        7        8 
## 3.314678 4.366959 2.592442 1.794065 4.692880 3.937164 3.246783 6.773392 
##        9 
## 9.297252
residual3=abs(residual)
model3new=lm(formula=residual3~X3,data = data)
summary(model3new)
## 
## Call:
## lm(formula = residual3 ~ X3, data = data)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -1.6039 -1.0955 -1.0751 -0.4915  4.5679 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)
## (Intercept)  13.4360     7.5924    1.77    0.120
## X3           -0.3034     0.2550   -1.19    0.273
## 
## Residual standard error: 2.246 on 7 degrees of freedom
## Multiple R-squared:  0.1682, Adjusted R-squared:  0.0494 
## F-statistic: 1.416 on 1 and 7 DF,  p-value: 0.2729
#ANALISIS RLB
library(lmtest)
model4=lm(Y~X1+X2+X3, data=data)
summary(model4)
## 
## Call:
## lm(formula = Y ~ X1 + X2 + X3, data = data)
## 
## Residuals:
##       1       2       3       4       5       6       7       8       9 
## -0.2981 -0.2142  0.4714 -0.4746 -0.9524  0.4761  0.9274  0.7466 -0.6823 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 21.87353    4.07389   5.369  0.00302 ** 
## X1           0.41277    0.03067  13.460 4.05e-05 ***
## X2           2.20267    0.47199   4.667  0.00550 ** 
## X3          -0.07895    0.15551  -0.508  0.63330    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.8484 on 5 degrees of freedom
## Multiple R-squared:  0.9888, Adjusted R-squared:  0.9821 
## F-statistic: 147.1 on 3 and 5 DF,  p-value: 2.696e-05
#UJI ASUMSI
library(car)
library(lmtest)
library(nortest)
#Uji Multikolinieritas
vif(model4)
##       X1       X2       X3 
## 1.403219 3.093549 2.607991
#Uji Normalitas
shapiro.test(resid(model4))
## 
##  Shapiro-Wilk normality test
## 
## data:  resid(model4)
## W = 0.93208, p-value = 0.5014
#Uji Heteroskedastisitas
bptest(model4, varformula= ~fitted.values(model4), studentize=FALSE)
## 
##  Breusch-Pagan test
## 
## data:  model4
## BP = 0.031878, df = 1, p-value = 0.8583
#Uji Autokorelasi
dwtest(model4, alternative='two.sided')
## 
##  Durbin-Watson test
## 
## data:  model4
## DW = 1.6446, p-value = 0.233
## alternative hypothesis: true autocorrelation is not 0
bgtest(model4)
## 
##  Breusch-Godfrey test for serial correlation of order up to 1
## 
## data:  model4
## LM test = 0.51249, df = 1, p-value = 0.4741
#UJI ASUMSI HETEROSKEDASITAS MENGGUNAKAN UJI GLEJSER (MODEL 4)
resid(model4)
##          1          2          3          4          5          6          7 
## -0.2980857 -0.2141744  0.4714083 -0.4745650 -0.9524106  0.4760829  0.9274363 
##          8          9 
##  0.7466056 -0.6822975
residual=resid(model4)
abs(residual)
##         1         2         3         4         5         6         7         8 
## 0.2980857 0.2141744 0.4714083 0.4745650 0.9524106 0.4760829 0.9274363 0.7466056 
##         9 
## 0.6822975
residual4=abs(residual)
model4new=lm(formula=residual4~X1+X2+X3,data = data)
summary(model4new)
## 
## Call:
## lm(formula = residual4 ~ X1 + X2 + X3, data = data)
## 
## Residuals:
##        1        2        3        4        5        6        7        8 
## -0.24968 -0.37078 -0.06495 -0.01987  0.35744 -0.17650  0.37477  0.12608 
##        9 
##  0.02347 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)
## (Intercept)  1.000238   1.548904   0.646    0.547
## X1           0.002391   0.011660   0.205    0.846
## X2           0.035130   0.179452   0.196    0.853
## X3          -0.024936   0.059127  -0.422    0.691
## 
## Residual standard error: 0.3226 on 5 degrees of freedom
## Multiple R-squared:  0.04467,    Adjusted R-squared:  -0.5285 
## F-statistic: 0.07793 on 3 and 5 DF,  p-value: 0.9692