Import Data

library (readr)
## Warning: package 'readr' was built under R version 4.5.3
path <- "C:\\Users\\ASUS\\Downloads\\Data Cleaning - Rumah 123.csv"

# 1. Baca mentah per baris (aman dari masalah encoding)
baris <- readLines(path, encoding = "latin1", warn = FALSE)

# 2. Buang tanda kutip yang membungkus satu baris penuh
baris <- gsub('"', "", baris)

# 3. Deteksi pemisah otomatis dari baris header
pemisah <- if (grepl(";", baris[1])) ";" else if (grepl("\t", baris[1])) "\t" else ","

# 4. Pecah jadi kolom, nama kolom dipertahankan
data <- read.table(
  text = paste(baris, collapse = "\n"),
  sep = pemisah, header = TRUE, quote = "",
  check.names = FALSE, strip.white = TRUE,
  stringsAsFactors = FALSE, fill = TRUE
)

# 5. Ubah kolom yang isinya angka jadi numerik
data[] <- lapply(data, type.convert, as.is = TRUE)

library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
data <- data %>% rename(Harga = `Harga (Miliar)`)
data <- data %>% rename(`Luas Tanah` = `Luas Tanah (m²)`)
data <- data %>% rename(`Luas Bangunan` = `Luas Bangunan (m²)`)
data <- data %>% rename(`Cicilan` = `Cicilan (Jutaan per Bulan)`)

str(data)
## 'data.frame':    1015 obs. of  10 variables:
##  $ Kamar Tidur   : int  2 2 2 2 2 2 2 2 2 2 ...
##  $ Kamar Mandi   : int  1 1 1 1 1 1 1 1 1 1 ...
##  $ Luas Tanah    : int  65 50 72 72 60 60 58 75 60 72 ...
##  $ Garasi        : int  0 1 0 0 1 1 0 0 0 0 ...
##  $ Luas Bangunan : int  35 30 50 50 45 32 45 60 45 36 ...
##  $ Jenis Bangunan: chr  "Rumah" "Rumah" "Rumah" "Rumah" ...
##  $ Harga         : num  0.3 0.353 0.36 0.36 0.43 0.45 0.47 0.47 0.47 0.48 ...
##  $ Cicilan       : num  1 1 1 1 1 1 1 1 1 1 ...
##  $ Kec           : chr  "Grand Wisata" "Jati Asih" "Grand Wisata" "Bekasi" ...
##  $ Kab/Kota      : chr  "Bekasi" "Bekasi" "Bekasi" "Bekasi" ...
head(data)
##   Kamar Tidur Kamar Mandi Luas Tanah Garasi Luas Bangunan Jenis Bangunan Harga
## 1           2           1         65      0            35          Rumah 0.300
## 2           2           1         50      1            30          Rumah 0.353
## 3           2           1         72      0            50          Rumah 0.360
## 4           2           1         72      0            50          Rumah 0.360
## 5           2           1         60      1            45          Rumah 0.430
## 6           2           1         60      1            32          Rumah 0.450
##   Cicilan          Kec Kab/Kota
## 1       1 Grand Wisata   Bekasi
## 2       1    Jati Asih   Bekasi
## 3       1 Grand Wisata   Bekasi
## 4       1       Bekasi   Bekasi
## 5       1 Bekasi Utara   Bekasi
## 6       1     Cibitung   Bekasi

Regresi Harga Rumah dengan 6 Peubah Prediktor

Model Regresi Awal

library(dplyr)
library(lmtest)
## Warning: package 'lmtest' was built under R version 4.5.3
## Loading required package: zoo
## Warning: package 'zoo' was built under R version 4.5.3
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
library(car)
## Warning: package 'car' was built under R version 4.5.3
## Loading required package: carData
## Warning: package 'carData' was built under R version 4.5.3
## 
## Attaching package: 'car'
## The following object is masked from 'package:dplyr':
## 
##     recode
model <- lm(Harga ~ `Kamar Tidur`+`Kamar Mandi`+`Luas Tanah`+`Garasi`+`Luas Bangunan`+`Cicilan`, data = data)

summary(model)
## 
## Call:
## lm(formula = Harga ~ `Kamar Tidur` + `Kamar Mandi` + `Luas Tanah` + 
##     Garasi + `Luas Bangunan` + Cicilan, data = data)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -107.13  -23.07  -10.37   -5.94  721.26 
## 
## Coefficients:
##                 Estimate Std. Error t value Pr(>|t|)    
## (Intercept)      8.11031    7.27568   1.115   0.2652    
## `Kamar Tidur`    3.53056    2.78285   1.269   0.2048    
## `Kamar Mandi`   -3.65197    1.91042  -1.912   0.0562 .  
## `Luas Tanah`    -0.07156    0.01824  -3.924 9.29e-05 ***
## Garasi          14.11149    2.43642   5.792 9.29e-09 ***
## `Luas Bangunan`  0.06055    0.01496   4.047 5.59e-05 ***
## Cicilan          0.02163    0.05331   0.406   0.6851    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 75.37 on 1008 degrees of freedom
## Multiple R-squared:  0.05407,    Adjusted R-squared:  0.04844 
## F-statistic: 9.603 on 6 and 1008 DF,  p-value: 2.728e-10
t.test(model$residuals,mu = 0,conf.level=0.95)
## 
##  One Sample t-test
## 
## data:  model$residuals
## t = -5.6503e-15, df = 1014, p-value = 1
## alternative hypothesis: true mean is not equal to 0
## 95 percent confidence interval:
##  -4.628822  4.628822
## sample estimates:
##     mean of x 
## -1.332836e-14
bptest(model)
## 
##  studentized Breusch-Pagan test
## 
## data:  model
## BP = 19.085, df = 6, p-value = 0.004023
ks.test(model$residuals, "pnorm", mean(model$residuals), sd(model$residuals))
## Warning in ks.test.default(model$residuals, "pnorm", mean(model$residuals), :
## ties should not be present for the one-sample Kolmogorov-Smirnov test
## 
##  Asymptotic one-sample Kolmogorov-Smirnov test
## 
## data:  model$residuals
## D = 0.39977, p-value < 2.2e-16
## alternative hypothesis: two-sided
shapiro.test(model$residuals)
## 
##  Shapiro-Wilk normality test
## 
## data:  model$residuals
## W = 0.38035, p-value < 2.2e-16
dwtest(model)
## 
##  Durbin-Watson test
## 
## data:  model
## DW = 1.4786, p-value < 2.2e-16
## alternative hypothesis: true autocorrelation is greater than 0
vif(model)
##   `Kamar Tidur`   `Kamar Mandi`    `Luas Tanah`          Garasi `Luas Bangunan` 
##        1.611145        3.902717        4.920269        1.289932        8.420083 
##         Cicilan 
##        1.077082

Penanganan Autokorelasi dengan Transformasi Boxcox

library(MASS)
## 
## Attaching package: 'MASS'
## The following object is masked from 'package:dplyr':
## 
##     select
boxcox_result <- boxcox(model)

lambda_optimal <- boxcox_result$x[which.max(boxcox_result$y)]

if (lambda_optimal != 0) {
    data$Harga_transformed <- (data$Harga^lambda_optimal - 1) / lambda_optimal
} else {
    data$Harga_transformed <- log(data$Harga)
}

model2 <- lm(Harga_transformed ~ `Kamar Tidur` + `Kamar Mandi` + `Luas Tanah`+`Luas Bangunan` + `Garasi` + `Jenis Bangunan` + `Cicilan`, data = data)

summary(model2)
## 
## Call:
## lm(formula = Harga_transformed ~ `Kamar Tidur` + `Kamar Mandi` + 
##     `Luas Tanah` + `Luas Bangunan` + Garasi + `Jenis Bangunan` + 
##     Cicilan, data = data)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -4.0901 -0.3811 -0.0069  0.3237  3.0137 
## 
## Coefficients:
##                               Estimate Std. Error t value Pr(>|t|)    
## (Intercept)                 -3.869e-01  1.784e-01  -2.169 0.030332 *  
## `Kamar Tidur`                3.387e-01  2.548e-02  13.295  < 2e-16 ***
## `Kamar Mandi`                7.587e-02  1.793e-02   4.233 2.52e-05 ***
## `Luas Tanah`                 8.368e-05  1.769e-04   0.473 0.636305    
## `Luas Bangunan`             -1.081e-04  1.474e-04  -0.734 0.463362    
## Garasi                       1.723e-01  2.208e-02   7.805 1.49e-14 ***
## `Jenis Bangunan`Gudang       8.120e-01  5.170e-01   1.571 0.116603    
## `Jenis Bangunan`Kantor      -2.061e+00  4.852e-01  -4.247 2.37e-05 ***
## `Jenis Bangunan`Ruang Usaha  2.294e+00  5.106e-01   4.493 7.85e-06 ***
## `Jenis Bangunan`Ruko         1.049e+00  2.910e-01   3.605 0.000328 ***
## `Jenis Bangunan`Rumah       -5.904e-01  1.750e-01  -3.373 0.000771 ***
## Cicilan                     -2.478e-04  5.020e-04  -0.494 0.621601    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.6687 on 1003 degrees of freedom
## Multiple R-squared:  0.4109, Adjusted R-squared:  0.4045 
## F-statistic: 63.61 on 11 and 1003 DF,  p-value: < 2.2e-16
t.test(model2$residuals,mu = 0,conf.level=0.95)
## 
##  One Sample t-test
## 
## data:  model2$residuals
## t = -1.5525e-15, df = 1014, p-value = 1
## alternative hypothesis: true mean is not equal to 0
## 95 percent confidence interval:
##  -0.0409618  0.0409618
## sample estimates:
##     mean of x 
## -3.240772e-17
bptest(model2)
## 
##  studentized Breusch-Pagan test
## 
## data:  model2
## BP = 78.473, df = 11, p-value = 2.911e-12
ks.test(model2$residuals, "pnorm", mean(model2$residuals), sd(model2$residuals))
## Warning in ks.test.default(model2$residuals, "pnorm", mean(model2$residuals), :
## ties should not be present for the one-sample Kolmogorov-Smirnov test
## 
##  Asymptotic one-sample Kolmogorov-Smirnov test
## 
## data:  model2$residuals
## D = 0.065591, p-value = 0.0003222
## alternative hypothesis: two-sided
shapiro.test(model2$residuals)
## 
##  Shapiro-Wilk normality test
## 
## data:  model2$residuals
## W = 0.95445, p-value < 2.2e-16
dwtest(model2)
## 
##  Durbin-Watson test
## 
## data:  model2
## DW = 1.2044, p-value < 2.2e-16
## alternative hypothesis: true autocorrelation is greater than 0
vif(model2)
##                       GVIF Df GVIF^(1/(2*Df))
## `Kamar Tidur`     1.715685  1        1.309842
## `Kamar Mandi`     4.365903  1        2.089474
## `Luas Tanah`      5.883165  1        2.425524
## `Luas Bangunan`  10.381453  1        3.222026
## Garasi            1.346276  1        1.160291
## `Jenis Bangunan`  2.864944  5        1.110994
## Cicilan           1.213280  1        1.101490

Log

model3 <- lm(log(Harga) ~ `Kamar Tidur` + `Kamar Mandi`+`Luas Bangunan` + `Luas Tanah` + `Garasi` + `Cicilan`, data = data)

summary(model3)
## 
## Call:
## lm(formula = log(Harga) ~ `Kamar Tidur` + `Kamar Mandi` + `Luas Bangunan` + 
##     `Luas Tanah` + Garasi + Cicilan, data = data)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -3.9960 -0.6779 -0.2424  0.2199  6.8021 
## 
## Coefficients:
##                   Estimate Std. Error t value Pr(>|t|)    
## (Intercept)     -0.8654437  0.1298035  -6.667 4.28e-11 ***
## `Kamar Tidur`    0.4114271  0.0496481   8.287 3.68e-16 ***
## `Kamar Mandi`    0.0196005  0.0340833   0.575   0.5654    
## `Luas Bangunan`  0.0005701  0.0002670   2.136   0.0329 *  
## `Luas Tanah`    -0.0005035  0.0003253  -1.547   0.1221    
## Garasi           0.3994598  0.0434675   9.190  < 2e-16 ***
## Cicilan          0.0024311  0.0009512   2.556   0.0107 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.345 on 1008 degrees of freedom
## Multiple R-squared:  0.2809, Adjusted R-squared:  0.2767 
## F-statistic: 65.64 on 6 and 1008 DF,  p-value: < 2.2e-16
t.test(model3$residuals,mu = 0,conf.level=0.95)
## 
##  One Sample t-test
## 
## data:  model3$residuals
## t = -6.8585e-16, df = 1014, p-value = 1
## alternative hypothesis: true mean is not equal to 0
## 95 percent confidence interval:
##  -0.08258154  0.08258154
## sample estimates:
##     mean of x 
## -2.886315e-17
dwtest(model3)
## 
##  Durbin-Watson test
## 
## data:  model3
## DW = 0.85921, p-value < 2.2e-16
## alternative hypothesis: true autocorrelation is greater than 0
bptest(model3)
## 
##  studentized Breusch-Pagan test
## 
## data:  model3
## BP = 63.89, df = 6, p-value = 7.27e-12
ks.test(model3$residuals, "pnorm", mean(model3$residuals), sd(model3$residuals))
## Warning in ks.test.default(model3$residuals, "pnorm", mean(model3$residuals), :
## ties should not be present for the one-sample Kolmogorov-Smirnov test
## 
##  Asymptotic one-sample Kolmogorov-Smirnov test
## 
## data:  model3$residuals
## D = 0.19342, p-value < 2.2e-16
## alternative hypothesis: two-sided
shapiro.test(model3$residuals)
## 
##  Shapiro-Wilk normality test
## 
## data:  model3$residuals
## W = 0.7839, p-value < 2.2e-16
vif(model3)
##   `Kamar Tidur`   `Kamar Mandi` `Luas Bangunan`    `Luas Tanah`          Garasi 
##        1.611145        3.902717        8.420083        4.920269        1.289932 
##         Cicilan 
##        1.077082

Sqrt

model4 <- lm(sqrt(Harga) ~ `Kamar Tidur` + `Kamar Mandi`+`Luas Bangunan` + `Luas Tanah` + `Garasi` + `Cicilan`, data = data)

summary(model4)
## 
## Call:
## lm(formula = sqrt(Harga) ~ `Kamar Tidur` + `Kamar Mandi` + `Luas Bangunan` + 
##     `Luas Tanah` + Garasi + Cicilan, data = data)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -6.6439 -1.3599 -0.6169 -0.2378 23.7160 
## 
## Coefficients:
##                   Estimate Std. Error t value Pr(>|t|)    
## (Intercept)      0.7290037  0.3479507   2.095  0.03641 *  
## `Kamar Tidur`    0.4077571  0.1330866   3.064  0.00224 ** 
## `Kamar Mandi`   -0.1279939  0.0913635  -1.401  0.16154    
## `Luas Bangunan`  0.0031224  0.0007156   4.363 1.41e-05 ***
## `Luas Tanah`    -0.0034230  0.0008721  -3.925 9.27e-05 ***
## Garasi           0.8885750  0.1165187   7.626 5.58e-14 ***
## Cicilan          0.0042834  0.0025497   1.680  0.09327 .  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 3.605 on 1008 degrees of freedom
## Multiple R-squared:  0.1237, Adjusted R-squared:  0.1185 
## F-statistic: 23.72 on 6 and 1008 DF,  p-value: < 2.2e-16
t.test(model4$residuals,mu = 0,conf.level=0.95)
## 
##  One Sample t-test
## 
## data:  model4$residuals
## t = -6.5295e-15, df = 1014, p-value = 1
## alternative hypothesis: true mean is not equal to 0
## 95 percent confidence interval:
##  -0.2213678  0.2213678
## sample estimates:
##     mean of x 
## -7.365938e-16
dwtest(model4)
## 
##  Durbin-Watson test
## 
## data:  model4
## DW = 1.0664, p-value < 2.2e-16
## alternative hypothesis: true autocorrelation is greater than 0
bptest(model4)
## 
##  studentized Breusch-Pagan test
## 
## data:  model4
## BP = 34.599, df = 6, p-value = 5.154e-06
ks.test(model4$residuals, "pnorm", mean(model4$residuals), sd(model4$residuals))
## Warning in ks.test.default(model4$residuals, "pnorm", mean(model4$residuals), :
## ties should not be present for the one-sample Kolmogorov-Smirnov test
## 
##  Asymptotic one-sample Kolmogorov-Smirnov test
## 
## data:  model4$residuals
## D = 0.34181, p-value < 2.2e-16
## alternative hypothesis: two-sided
shapiro.test(model4$residuals)
## 
##  Shapiro-Wilk normality test
## 
## data:  model4$residuals
## W = 0.51364, p-value < 2.2e-16
vif(model4)
##   `Kamar Tidur`   `Kamar Mandi` `Luas Bangunan`    `Luas Tanah`          Garasi 
##        1.611145        3.902717        8.420083        4.920269        1.289932 
##         Cicilan 
##        1.077082

Reciprocal

model5 <- lm(1/Harga ~ `Kamar Tidur` + `Kamar Mandi` + `Luas Tanah` + `Luas Bangunan`+`Garasi` + `Cicilan`, data = data)

summary(model5)
## 
## Call:
## lm(formula = 1/Harga ~ `Kamar Tidur` + `Kamar Mandi` + `Luas Tanah` + 
##     `Luas Bangunan` + Garasi + Cicilan, data = data)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -1.7923 -0.3720 -0.1098  0.2512  9.0595 
## 
## Coefficients:
##                   Estimate Std. Error t value Pr(>|t|)    
## (Intercept)      1.8672569  0.0649066  28.768  < 2e-16 ***
## `Kamar Tidur`   -0.2747095  0.0248259 -11.065  < 2e-16 ***
## `Kamar Mandi`   -0.0549269  0.0170429  -3.223 0.001310 ** 
## `Luas Tanah`    -0.0004329  0.0001627  -2.661 0.007910 ** 
## `Luas Bangunan`  0.0003970  0.0001335   2.974 0.003009 ** 
## Garasi          -0.0861586  0.0217354  -3.964 7.89e-05 ***
## Cicilan          0.0017050  0.0004756   3.585 0.000353 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.6724 on 1008 degrees of freedom
## Multiple R-squared:  0.2539, Adjusted R-squared:  0.2494 
## F-statistic: 57.17 on 6 and 1008 DF,  p-value: < 2.2e-16
t.test(model5$residuals,mu = 0,conf.level=0.95)
## 
##  One Sample t-test
## 
## data:  model5$residuals
## t = 2.1145e-15, df = 1014, p-value = 1
## alternative hypothesis: true mean is not equal to 0
## 95 percent confidence interval:
##  -0.04129388  0.04129388
## sample estimates:
##    mean of x 
## 4.449705e-17
dwtest(model5)
## 
##  Durbin-Watson test
## 
## data:  model5
## DW = 1.3707, p-value < 2.2e-16
## alternative hypothesis: true autocorrelation is greater than 0
bptest(model5)
## 
##  studentized Breusch-Pagan test
## 
## data:  model5
## BP = 2.9529, df = 6, p-value = 0.8147
ks.test(model5$residuals, "pnorm", mean(model5$residuals), sd(model5$residuals))
## Warning in ks.test.default(model5$residuals, "pnorm", mean(model5$residuals), :
## ties should not be present for the one-sample Kolmogorov-Smirnov test
## 
##  Asymptotic one-sample Kolmogorov-Smirnov test
## 
## data:  model5$residuals
## D = 0.11148, p-value = 2.211e-11
## alternative hypothesis: two-sided
shapiro.test(model5$residuals)
## 
##  Shapiro-Wilk normality test
## 
## data:  model5$residuals
## W = 0.77976, p-value < 2.2e-16
vif(model5)
##   `Kamar Tidur`   `Kamar Mandi`    `Luas Tanah` `Luas Bangunan`          Garasi 
##        1.611145        3.902717        4.920269        8.420083        1.289932 
##         Cicilan 
##        1.077082

Kuadrat

model6 <- lm(I(Harga^2) ~ `Kamar Tidur` + `Kamar Mandi` + `Luas Tanah` + `Garasi` + `Luas Bangunan` + `Cicilan`, data = data)

summary(model6)
## 
## Call:
## lm(formula = I(Harga^2) ~ `Kamar Tidur` + `Kamar Mandi` + `Luas Tanah` + 
##     Garasi + `Luas Bangunan` + Cicilan, data = data)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
## -37887  -8566  -4097  -2438 573655 
## 
## Coefficients:
##                  Estimate Std. Error t value Pr(>|t|)    
## (Intercept)      4301.444   3672.522   1.171  0.24177    
## `Kamar Tidur`     985.926   1404.692   0.702  0.48292    
## `Kamar Mandi`   -1774.895    964.316  -1.841  0.06598 .  
## `Luas Tanah`      -25.248      9.205  -2.743  0.00620 ** 
## Garasi           4912.488   1229.822   3.994 6.96e-05 ***
## `Luas Bangunan`    20.547      7.553   2.720  0.00663 ** 
## Cicilan            -3.298     26.912  -0.123  0.90249    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 38050 on 1008 degrees of freedom
## Multiple R-squared:  0.02087,    Adjusted R-squared:  0.01505 
## F-statistic: 3.582 on 6 and 1008 DF,  p-value: 0.001616
t.test(model6$residuals,mu = 0,conf.level=0.95)
## 
##  One Sample t-test
## 
## data:  model6$residuals
## t = 1.6557e-15, df = 1014, p-value = 1
## alternative hypothesis: true mean is not equal to 0
## 95 percent confidence interval:
##  -2336.475  2336.475
## sample estimates:
##    mean of x 
## 1.971401e-12
dwtest(model6)
## 
##  Durbin-Watson test
## 
## data:  model6
## DW = 1.9168, p-value = 0.08873
## alternative hypothesis: true autocorrelation is greater than 0
bptest(model6)
## 
##  studentized Breusch-Pagan test
## 
## data:  model6
## BP = 8.0494, df = 6, p-value = 0.2345
ks.test(model6$residuals, "pnorm", mean(model6$residuals), sd(model6$residuals))
## Warning in ks.test.default(model6$residuals, "pnorm", mean(model6$residuals), :
## ties should not be present for the one-sample Kolmogorov-Smirnov test
## 
##  Asymptotic one-sample Kolmogorov-Smirnov test
## 
## data:  model6$residuals
## D = 0.42272, p-value < 2.2e-16
## alternative hypothesis: two-sided
shapiro.test(model6$residuals)
## 
##  Shapiro-Wilk normality test
## 
## data:  model6$residuals
## W = 0.24236, p-value < 2.2e-16
vif(model6)
##   `Kamar Tidur`   `Kamar Mandi`    `Luas Tanah`          Garasi `Luas Bangunan` 
##        1.611145        3.902717        4.920269        1.289932        8.420083 
##         Cicilan 
##        1.077082
# Ringkasan model untuk mendapatkan Adjusted R-squared
summary_model6 <- summary(model6)

# Menampilkan Adjusted R-squared
adjusted_r_squared <- summary_model6$adj.r.squared
cat("Adjusted R-squared: ", adjusted_r_squared, "\n")
## Adjusted R-squared:  0.01504579
# Menghitung Residual Standard Error (RSE)
n <- length(model6$residuals)  # Jumlah observasi
p <- length(coef(model6))  # Jumlah parameter (koefisien)
rss <- sum(model6$residuals^2)  # Residual sum of squares

rse <- sqrt(rss / (n - p))  # Residual Standard Error
cat("Residual Standard Error (RSE): ", rse, "\n")
## Residual Standard Error (RSE):  38046.61

Penanganan dengan Membuang Outlier

influence <- influence.measures(model)
summary(influence)
## Potentially influential observations of
##   lm(formula = Harga ~ `Kamar Tidur` + `Kamar Mandi` + `Luas Tanah` +      Garasi + `Luas Bangunan` + Cicilan, data = data) :
## 
##     dfb.1_ dfb.`KT` dfb.`KM` dfb.`LT` dfb.Gars dfb.`LB` dfb.Ccln dffit  
## 101  0.02  -0.03     0.01     0.01     0.01    -0.01     0.00    -0.04  
## 141  0.03  -0.04     0.01     0.01     0.02     0.00     0.00    -0.04  
## 157 -0.01   0.00     0.01     0.02     0.00    -0.01     0.00     0.02  
## 204  0.00   0.08    -0.02    -0.07    -0.38     0.06     0.07    -0.43_*
## 348 -0.48   0.25     0.39     0.91    -0.45    -0.77    -0.02     1.04_*
## 349 -0.03  -0.07     0.20     0.28    -0.12    -0.25     0.03     0.30_*
## 355 -0.01  -0.01     0.04     0.08    -0.01    -0.07     0.00     0.08  
## 358 -0.01  -0.01     0.04     0.08    -0.01    -0.07     0.00     0.08  
## 446 -0.38  -0.03     1.36_*  -0.44     0.17    -1.03_*   0.39    -2.66_*
## 482  0.02  -0.01    -0.02    -0.03     0.00     0.03     0.00    -0.04  
## 508  0.01  -0.03     0.05     0.01     0.02    -0.04     0.00    -0.08  
## 574  0.01  -0.01     0.01     0.01     0.01    -0.02    -0.01    -0.03  
## 583  0.03   0.04    -0.03    -0.12    -0.15     0.09    -0.01    -0.24  
## 590  0.00   0.04     0.02    -0.02    -0.25     0.01     0.04    -0.28_*
## 591  0.02  -0.03     0.01     0.02     0.00    -0.02    -0.03    -0.04  
## 593  0.01  -0.02     0.00     0.05     0.01    -0.05     0.01    -0.10  
## 620 -0.03   0.04    -0.02     0.01     0.04     0.01    -0.24    -0.24  
## 632  0.00   0.01    -0.02    -0.03    -0.02     0.03    -0.01    -0.05  
## 643  0.09  -0.21     0.20     0.47    -0.15    -0.31    -0.04     0.51_*
## 644  0.22  -0.31     0.18     0.14     0.06    -0.17     0.04    -0.34_*
## 656  0.01  -0.02     0.02     0.02     0.01    -0.02    -0.02    -0.03  
## 663  0.03   0.01    -0.03    -0.05     0.03     0.04    -0.01     0.10  
## 665  0.00  -0.01     0.02     0.02    -0.01    -0.03    -0.03    -0.05  
## 668 -0.05   0.06    -0.03     0.01     0.06     0.02    -0.40    -0.40_*
## 669 -0.08   0.17    -0.13    -0.15    -0.05     0.18    -0.03     0.26_*
## 671 -0.06   0.05     0.08     0.02    -0.01    -0.07    -0.01     0.16  
## 674  0.00   0.00     0.00     0.00     0.00     0.00    -0.01    -0.01  
## 675 -0.08   0.16    -0.13    -0.15     0.20     0.14    -0.02     0.34_*
## 677  0.42  -0.35    -0.09     0.05     0.12     0.10    -0.05     0.49_*
## 679  0.01   0.00     0.00     0.00    -0.02     0.00    -0.03    -0.04  
## 680 -0.10   0.20    -0.10    -0.10     0.01     0.09     0.01     0.29_*
## 682  0.41  -0.35    -0.11     0.01     0.24     0.12    -0.06     0.51_*
## 686  0.00   0.00     0.00     0.00     0.00     0.00     0.00     0.00  
## 687 -0.15   0.17     0.01     0.01    -0.04    -0.03    -0.02     0.22  
## 688  0.04  -0.02     0.02     0.00     0.02     0.00    -0.01     0.11  
## 689  0.04   0.02    -0.06    -0.05     0.04     0.06    -0.01     0.12  
## 690  0.03  -0.06     0.13     0.06     0.00    -0.10     0.00     0.16  
## 691 -0.05   0.06     0.04    -0.02     0.00    -0.02    -0.01     0.14  
## 694 -0.06   0.06     0.03    -0.04     0.12     0.00    -0.02     0.21  
## 699 -0.12   0.16     0.02     0.02    -0.03    -0.06     0.01     0.27_*
## 700 -0.03   0.03    -0.01    -0.07     0.19     0.04    -0.04     0.24  
## 701  0.00   0.00     0.00     0.00     0.00     0.00    -0.01    -0.01  
## 704 -0.23   0.28     0.01    -0.13    -0.03     0.05     0.00     0.39_*
## 705  0.01  -0.02     0.02     0.02    -0.01    -0.02    -0.03    -0.05  
## 707 -0.09   0.13    -0.06    -0.08     0.17     0.05    -0.02     0.31_*
## 708 -0.02   0.17    -0.29     0.08    -0.01    -0.10     0.04    -0.64_*
## 713  0.00   0.00     0.00     0.01     0.00     0.00     0.00     0.01  
## 714  0.81  -0.72    -0.06     0.08     0.24     0.09     0.06     0.88_*
## 718 -0.12   0.36    -0.40    -0.25     0.05     0.31     0.00     0.50_*
## 720 -0.45   0.38     0.29     0.19    -0.18    -0.31     0.07     0.71_*
## 724 -0.06   0.17    -0.18    -0.25     0.21     0.22    -0.03     0.37_*
## 725  0.02   0.04    -0.08    -0.06     0.04     0.06    -0.03     0.12  
## 727 -0.07   0.17    -0.16    -0.23     0.22     0.20    -0.02     0.38_*
## 731  0.04   0.13    -0.24    -0.17    -0.02     0.22    -0.03     0.28_*
## 735 -0.10   0.12    -0.01    -0.03     0.16    -0.01    -0.02     0.30_*
## 736 -0.03   0.03    -0.01    -0.07     0.19     0.04    -0.04     0.24  
## 737  0.00   0.00     0.00     0.00     0.00     0.00    -0.01    -0.01  
## 740 -0.23   0.28     0.01    -0.13    -0.03     0.05     0.00     0.39_*
## 741  0.01  -0.02     0.02     0.02    -0.01    -0.02    -0.03    -0.05  
## 743 -0.09   0.13    -0.06    -0.08     0.17     0.05    -0.02     0.31_*
## 744  0.00   0.05    -0.09     0.02     0.02    -0.03     0.01    -0.20  
## 749  0.00   0.00     0.00     0.01     0.00     0.00     0.00     0.01  
## 750  0.82  -0.77    -0.14    -0.07     0.72     0.20    -0.02     1.09_*
## 752  0.08   0.24    -0.48    -0.33    -0.04     0.43    -0.05     0.55_*
## 755  0.02   0.00     0.00    -0.01    -0.11    -0.01    -0.02    -0.14  
## 756  0.18  -0.07    -0.14    -0.33     0.43     0.29     0.06     0.60_*
## 757  0.03  -0.03     0.01    -0.03     0.10     0.01    -0.02     0.13  
## 759  0.03  -0.01    -0.04    -0.02     0.09     0.03    -0.03     0.12  
## 760  0.05   0.00     0.00     0.03     0.03    -0.03     0.01     0.18  
## 764  0.13  -0.10     0.00    -0.03     0.06     0.03     0.00     0.15  
## 766  0.03   0.00    -0.04    -0.06     0.11     0.05    -0.02     0.14  
## 767  0.04   0.00    -0.05    -0.07     0.13     0.05    -0.02     0.16  
## 930  0.03  -0.01    -0.04    -0.02    -0.01     0.03     0.00    -0.06  
## 946  0.09  -0.01    -0.01    -0.11    -0.32     0.10     0.07    -0.41_*
##     cov.r   cook.d  hat    
## 101  1.02_*  0.00    0.01  
## 141  1.03_*  0.00    0.02_*
## 157  1.03_*  0.00    0.02_*
## 204  1.08_*  0.03    0.08_*
## 348  1.32_*  0.15    0.25_*
## 349  1.09_*  0.01    0.08_*
## 355  1.05_*  0.00    0.04_*
## 358  1.05_*  0.00    0.04_*
## 446  3.56_*  1.01_*  0.72_*
## 482  1.06_*  0.00    0.05_*
## 508  1.04_*  0.00    0.04_*
## 574  1.03_*  0.00    0.02_*
## 583  1.12_*  0.01    0.10_*
## 590  1.05_*  0.01    0.05_*
## 591  1.02_*  0.00    0.02  
## 593  1.05_*  0.00    0.04_*
## 620  1.46_*  0.01    0.31_*
## 632  1.05_*  0.00    0.04_*
## 643  1.14_*  0.04    0.13_*
## 644  1.14_*  0.02    0.12_*
## 656  1.03_*  0.00    0.02_*
## 663  0.96_*  0.00    0.00  
## 665  1.05_*  0.00    0.04_*
## 668  1.76_*  0.02    0.43_*
## 669  0.99    0.01    0.01  
## 671  0.95_*  0.00    0.00  
## 674  1.03_*  0.00    0.02  
## 675  0.79_*  0.02    0.00  
## 677  0.92_*  0.03    0.02  
## 679  1.03_*  0.00    0.02_*
## 680  0.78_*  0.01    0.00  
## 682  0.93_*  0.04    0.02  
## 686  1.02_*  0.00    0.01  
## 687  0.95_*  0.01    0.00  
## 688  0.94_*  0.00    0.00  
## 689  0.94_*  0.00    0.00  
## 690  0.95_*  0.00    0.00  
## 691  0.96_*  0.00    0.00  
## 694  0.91_*  0.01    0.00  
## 699  0.79_*  0.01    0.00  
## 700  0.95_*  0.01    0.01  
## 701  1.11_*  0.00    0.09_*
## 704  0.84_*  0.02    0.01  
## 705  1.02_*  0.00    0.02  
## 707  0.81_*  0.01    0.00  
## 708  1.77_*  0.06    0.43_*
## 713  1.05_*  0.00    0.04_*
## 714  0.67_*  0.10    0.01  
## 718  0.74_*  0.03    0.01  
## 720  0.59_*  0.07    0.01  
## 724  0.84_*  0.02    0.00  
## 725  0.97_*  0.00    0.00  
## 727  0.80_*  0.02    0.00  
## 731  0.94_*  0.01    0.01  
## 735  0.81_*  0.01    0.00  
## 736  0.95_*  0.01    0.01  
## 737  1.11_*  0.00    0.09_*
## 740  0.84_*  0.02    0.01  
## 741  1.02_*  0.00    0.02  
## 743  0.81_*  0.01    0.00  
## 744  1.78_*  0.01    0.44_*
## 749  1.05_*  0.00    0.04_*
## 750  0.70_*  0.16    0.02  
## 752  0.77_*  0.04    0.01  
## 755  1.03_*  0.00    0.02_*
## 756  0.52_*  0.05    0.00  
## 757  0.97_*  0.00    0.00  
## 759  0.97_*  0.00    0.00  
## 760  0.83_*  0.00    0.00  
## 764  0.95_*  0.00    0.00  
## 766  0.97_*  0.00    0.00  
## 767  0.95_*  0.00    0.00  
## 930  1.02_*  0.00    0.02  
## 946  1.08_*  0.02    0.08_*
outliers <- which(influence$infmat[, "hat"] > (2 * mean(influence$infmat[, "hat"])))
data_clean <- data[-outliers, ]

model7 <- lm(Harga ~ `Kamar Tidur` + `Kamar Mandi` + `Luas Tanah`+`Luas Bangunan` + `Garasi` + `Cicilan`, data = data_clean)

summary(model7)
## 
## Call:
## lm(formula = Harga ~ `Kamar Tidur` + `Kamar Mandi` + `Luas Tanah` + 
##     `Luas Bangunan` + Garasi + Cicilan, data = data_clean)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -124.77  -22.46   -8.95   -0.02  691.56 
## 
## Coefficients:
##                 Estimate Std. Error t value Pr(>|t|)    
## (Intercept)      2.34040    8.70005   0.269    0.788    
## `Kamar Tidur`    3.98904    3.88762   1.026    0.305    
## `Kamar Mandi`   -2.61256    3.35308  -0.779    0.436    
## `Luas Tanah`    -0.20400    0.04375  -4.663 3.56e-06 ***
## `Luas Bangunan`  0.17681    0.03801   4.652 3.74e-06 ***
## Garasi          13.50445    3.11676   4.333 1.63e-05 ***
## Cicilan          0.48938    0.33893   1.444    0.149    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 72.21 on 964 degrees of freedom
## Multiple R-squared:  0.08739,    Adjusted R-squared:  0.08171 
## F-statistic: 15.39 on 6 and 964 DF,  p-value: < 2.2e-16
t.test(model7$residuals,mu = 0,conf.level=0.95)
## 
##  One Sample t-test
## 
## data:  model7$residuals
## t = -6.7414e-16, df = 970, p-value = 1
## alternative hypothesis: true mean is not equal to 0
## 95 percent confidence interval:
##  -4.533338  4.533338
## sample estimates:
##     mean of x 
## -1.557309e-15
dwtest(model7)
## 
##  Durbin-Watson test
## 
## data:  model7
## DW = 1.4824, p-value = 2.505e-16
## alternative hypothesis: true autocorrelation is greater than 0
bptest(model7)
## 
##  studentized Breusch-Pagan test
## 
## data:  model7
## BP = 42.271, df = 6, p-value = 1.626e-07
ks.test(model7$residuals, "pnorm", mean(model7$residuals), sd(model7$residuals))
## Warning in ks.test.default(model7$residuals, "pnorm", mean(model7$residuals), :
## ties should not be present for the one-sample Kolmogorov-Smirnov test
## 
##  Asymptotic one-sample Kolmogorov-Smirnov test
## 
## data:  model7$residuals
## D = 0.34814, p-value < 2.2e-16
## alternative hypothesis: two-sided
shapiro.test(model7$residuals)
## 
##  Shapiro-Wilk normality test
## 
## data:  model7$residuals
## W = 0.4374, p-value < 2.2e-16

Kuadrat Non Outlier

model8 <- lm(1/Harga ~ `Kamar Tidur` + `Kamar Mandi` + `Luas Tanah` + `Garasi` + `Luas Bangunan` + `Cicilan`, data = data_clean)

summary(model8)
## 
## Call:
## lm(formula = 1/Harga ~ `Kamar Tidur` + `Kamar Mandi` + `Luas Tanah` + 
##     Garasi + `Luas Bangunan` + Cicilan, data = data_clean)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -1.6931 -0.3056 -0.0660  0.1994  9.0184 
## 
## Coefficients:
##                   Estimate Std. Error t value Pr(>|t|)    
## (Intercept)      1.9871235  0.0753193  26.383  < 2e-16 ***
## `Kamar Tidur`   -0.1652884  0.0336564  -4.911 1.06e-06 ***
## `Kamar Mandi`   -0.2297790  0.0290287  -7.916 6.74e-15 ***
## `Luas Tanah`    -0.0004239  0.0003787  -1.119  0.26335    
## Garasi          -0.0075801  0.0269828  -0.281  0.77883    
## `Luas Bangunan`  0.0001028  0.0003290   0.313  0.75471    
## Cicilan         -0.0081541  0.0029342  -2.779  0.00556 ** 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.6251 on 964 degrees of freedom
## Multiple R-squared:  0.3245, Adjusted R-squared:  0.3202 
## F-statistic: 77.16 on 6 and 964 DF,  p-value: < 2.2e-16
t.test(model8$residuals,mu = 0,conf.level=0.95)
## 
##  One Sample t-test
## 
## data:  model8$residuals
## t = -1.8484e-15, df = 970, p-value = 1
## alternative hypothesis: true mean is not equal to 0
## 95 percent confidence interval:
##  -0.03924663  0.03924663
## sample estimates:
##     mean of x 
## -3.696674e-17
dwtest(model8)
## 
##  Durbin-Watson test
## 
## data:  model8
## DW = 1.4579, p-value < 2.2e-16
## alternative hypothesis: true autocorrelation is greater than 0
bptest(model8)
## 
##  studentized Breusch-Pagan test
## 
## data:  model8
## BP = 10.205, df = 6, p-value = 0.1163
ks.test(model8$residuals, "pnorm", mean(model8$residuals), sd(model8$residuals))
## Warning in ks.test.default(model8$residuals, "pnorm", mean(model8$residuals), :
## ties should not be present for the one-sample Kolmogorov-Smirnov test
## 
##  Asymptotic one-sample Kolmogorov-Smirnov test
## 
## data:  model8$residuals
## D = 0.12987, p-value = 1.191e-14
## alternative hypothesis: two-sided
shapiro.test(model8$residuals)
## 
##  Shapiro-Wilk normality test
## 
## data:  model8$residuals
## W = 0.72478, p-value < 2.2e-16
vif(model8)
##   `Kamar Tidur`   `Kamar Mandi`    `Luas Tanah`          Garasi `Luas Bangunan` 
##        2.243711        2.267869        2.591888        1.295912        3.374868 
##         Cicilan 
##        2.758751

Regresi Lasso

# Install glmnet if necessary
library(glmnet)
## Warning: package 'glmnet' was built under R version 4.5.3
## Loading required package: Matrix
## Loaded glmnet 5.1
# Misalkan data sudah ada dalam variabel x (prediktor) dan y (respons)
x <- model.matrix(I(Harga^2) ~ `Kamar Tidur` + `Kamar Mandi` + `Luas Tanah`+`Luas Bangunan` + Garasi + Cicilan, data=data)[,-1]
y <- data$Harga

# Lakukan regresi Lasso
lasso_model <- glmnet(x, y, alpha = 1)

# Untuk cross-validation dan memilih nilai lambda yang optimal
cv_lasso <- cv.glmnet(x, y, alpha = 1)
best_lambda <- cv_lasso$lambda.min

# Lihat koefisien model pada lambda terbaik
coef(lasso_model, s = best_lambda)
## 7 x 1 sparse Matrix of class "dgCMatrix"
##                   s=1.045058
## (Intercept)      9.054449199
## `Kamar Tidur`    0.747238204
## `Kamar Mandi`    .          
## `Luas Tanah`    -0.027414594
## `Luas Bangunan`  0.024309742
## Garasi          11.813302110
## Cicilan          0.004905426
# Variabel yang dipilih oleh Lasso (koefisien tidak nol)
selected_vars <- rownames(coef(lasso_model, s = best_lambda))[which(coef(lasso_model, s = best_lambda) != 0)]

# Membuat model linier berdasarkan variabel yang dipilih oleh Lasso
formula_lasso <- as.formula(paste("Harga ~", paste(selected_vars[-1], collapse = " + ")))  # -1 untuk menghapus intercept
ols_model <- lm(formula_lasso, data = data_clean)

# Lihat ringkasan model OLS
summary(ols_model)
## 
## Call:
## lm(formula = formula_lasso, data = data_clean)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -120.54  -22.83   -9.16   -0.09  691.43 
## 
## Coefficients:
##                 Estimate Std. Error t value Pr(>|t|)    
## (Intercept)       2.0729     8.6915   0.239    0.812    
## `Kamar Tidur`     2.3969     3.3066   0.725    0.469    
## `Luas Tanah`     -0.2025     0.0437  -4.634 4.08e-06 ***
## `Luas Bangunan`   0.1720     0.0375   4.587 5.08e-06 ***
## Garasi           13.1424     3.0813   4.265 2.19e-05 ***
## Cicilan           0.4661     0.3375   1.381    0.168    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 72.19 on 965 degrees of freedom
## Multiple R-squared:  0.08682,    Adjusted R-squared:  0.08208 
## F-statistic: 18.35 on 5 and 965 DF,  p-value: < 2.2e-16
t.test(ols_model$residuals,mu = 0,conf.level=0.95)
## 
##  One Sample t-test
## 
## data:  ols_model$residuals
## t = -3.8831e-15, df = 970, p-value = 1
## alternative hypothesis: true mean is not equal to 0
## 95 percent confidence interval:
##  -4.534765  4.534765
## sample estimates:
##     mean of x 
## -8.973089e-15
dwtest(ols_model)
## 
##  Durbin-Watson test
## 
## data:  ols_model
## DW = 1.4801, p-value < 2.2e-16
## alternative hypothesis: true autocorrelation is greater than 0
bptest(ols_model)
## 
##  studentized Breusch-Pagan test
## 
## data:  ols_model
## BP = 40.592, df = 5, p-value = 1.134e-07
ks.test(ols_model$residuals, "pnorm", mean(ols_model$residuals), sd(ols_model$residuals))
## Warning in ks.test.default(ols_model$residuals, "pnorm",
## mean(ols_model$residuals), : ties should not be present for the one-sample
## Kolmogorov-Smirnov test
## 
##  Asymptotic one-sample Kolmogorov-Smirnov test
## 
## data:  ols_model$residuals
## D = 0.34993, p-value < 2.2e-16
## alternative hypothesis: two-sided
shapiro.test(ols_model$residuals)
## 
##  Shapiro-Wilk normality test
## 
## data:  ols_model$residuals
## W = 0.43667, p-value < 2.2e-16

Regresi robust

library(robustbase)
## Warning: package 'robustbase' was built under R version 4.5.3
# Misalkan data sudah ada dalam variabel x (prediktor) dan y (respons)
x <- model.matrix(I(Harga^2) ~ `Kamar Tidur` + `Kamar Mandi` + `Luas Tanah` +`Luas Bangunan`+ Garasi + Cicilan, data=data)[,-1]
y <- data$Harga

# Membuat model regresi robust
robust_model <- lmrob(Harga ~ `Kamar Tidur` + `Kamar Mandi` + `Luas Tanah` +`Luas Bangunan`+ Garasi + Cicilan, data=data_clean)

# Ringkasan hasil model robust
summary(robust_model)
## 
## Call:
## lmrob(formula = Harga ~ `Kamar Tidur` + `Kamar Mandi` + `Luas Tanah` + `Luas Bangunan` + 
##     Garasi + Cicilan, data = data_clean)
##  \--> method = "MM"
## Residuals:
##        Min         1Q     Median         3Q        Max 
## -19.802649  -0.061399   0.009579   0.084637 758.817336 
## 
## Coefficients:
##                   Estimate Std. Error t value Pr(>|t|)    
## (Intercept)      1.287e-01  1.159e-02  11.103   <2e-16 ***
## `Kamar Tidur`   -1.882e-03  5.491e-03  -0.343   0.7319    
## `Kamar Mandi`    2.016e-03  4.609e-03   0.437   0.6619    
## `Luas Tanah`    -6.476e-05  6.782e-05  -0.955   0.3399    
## `Luas Bangunan`  2.673e-04  1.151e-04   2.322   0.0204 *  
## Garasi           3.691e-03  3.974e-03   0.929   0.3533    
## Cicilan          2.658e-01  9.319e-04 285.206   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Robust residual standard error: 0.1033 
## Multiple R-squared:  0.9983, Adjusted R-squared:  0.9983 
## Convergence in 13 IRWLS iterations
## 
## Robustness weights: 
##  101 observations c(173,174,175,185,213,216,217,231,232,427,526,548,549,599,618,637,638,640,641,642,643,644,645,646,647,648,649,650,651,652,653,654,655,656,657,658,659,660,661,662,663,664,665,666,667,668,669,670,671,672,673,674,675,676,677,678,680,682,683,684,685,686,687,688,689,690,691,692,693,694,695,696,697,698,699,700,701,702,703,704,705,706,707,708,709,710,713,714,715,716,717,718,719,720,721,722,723,724,725,726,727)
##   are outliers with |weight| = 0 ( < 0.0001); 
##  67 weights are ~= 1. The remaining 803 ones are summarized as
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
## 0.08723 0.91960 0.95710 0.94100 0.98820 0.99890 
## Algorithmic parameters: 
##        tuning.chi                bb        tuning.psi        refine.tol 
##         1.548e+00         5.000e-01         4.685e+00         1.000e-07 
##           rel.tol         scale.tol         solve.tol          zero.tol 
##         1.000e-07         1.000e-10         1.000e-07         1.000e-10 
##       eps.outlier             eps.x warn.limit.reject warn.limit.meanrw 
##         1.030e-04         1.637e-09         5.000e-01         5.000e-01 
##      nResample         max.it       best.r.s       k.fast.s          k.max 
##            500             50              2              1            200 
##    maxit.scale      trace.lev            mts     compute.rd fast.s.large.n 
##            200              0           1000              0           2000 
##                   psi           subsampling                   cov 
##            "bisquare"         "nonsingular"         ".vcov.avar1" 
## compute.outlier.stats 
##                  "SM" 
## seed : int(0)
dwtest(robust_model)
## 
##  Durbin-Watson test
## 
## data:  robust_model
## DW = 1.4824, p-value = 2.505e-16
## alternative hypothesis: true autocorrelation is greater than 0
bptest(robust_model)
## 
##  studentized Breusch-Pagan test
## 
## data:  robust_model
## BP = 42.271, df = 6, p-value = 1.626e-07
ks.test(robust_model$residuals, "pnorm", mean(robust_model$residuals), sd(robust_model$residuals))
## Warning in ks.test.default(robust_model$residuals, "pnorm",
## mean(robust_model$residuals), : ties should not be present for the one-sample
## Kolmogorov-Smirnov test
## 
##  Asymptotic one-sample Kolmogorov-Smirnov test
## 
## data:  robust_model$residuals
## D = 0.49597, p-value < 2.2e-16
## alternative hypothesis: two-sided
shapiro.test(robust_model$residuals)
## 
##  Shapiro-Wilk normality test
## 
## data:  robust_model$residuals
## W = 0.24031, p-value < 2.2e-16

Regresi Quantile

library(quantreg)
## Warning: package 'quantreg' was built under R version 4.5.3
## Loading required package: SparseM
## Warning: package 'SparseM' was built under R version 4.5.3
## 
## Attaching package: 'SparseM'
## The following object is masked from 'package:Matrix':
## 
##     det
quantile_model <- rq(I(Harga^2) ~ `Kamar Tidur` + `Kamar Mandi` + `Luas Tanah`+`Luas Bangunan` + Garasi + Cicilan, data = data, tau = 0.5)  # Median regression
summary(quantile_model)
## Warning in summary.rq(quantile_model): 266 non-positive fis
## 
## Call: rq(formula = I(Harga^2) ~ `Kamar Tidur` + `Kamar Mandi` + `Luas Tanah` + 
##     `Luas Bangunan` + Garasi + Cicilan, tau = 0.5, data = data)
## 
## tau: [1] 0.5
## 
## Coefficients:
##                 Value    Std. Error t value  Pr(>|t|)
## (Intercept)      8.07042 14.39373    0.56069  0.57513
## `Kamar Tidur`   -8.47098  9.92308   -0.85366  0.39349
## `Kamar Mandi`   -3.82215  1.94283   -1.96731  0.04942
## `Luas Tanah`    -0.04061  0.08515   -0.47691  0.63353
## `Luas Bangunan`  0.21066  0.26389    0.79827  0.42490
## Garasi           1.33353  1.44286    0.92423  0.35559
## Cicilan          2.63520  2.62065    1.00555  0.31487
dwtest(quantile_model)
## 
##  Durbin-Watson test
## 
## data:  quantile_model
## DW = 1.9168, p-value = 0.08873
## alternative hypothesis: true autocorrelation is greater than 0
bptest(quantile_model)
## 
##  studentized Breusch-Pagan test
## 
## data:  quantile_model
## BP = 8.0494, df = 6, p-value = 0.2345
ks.test(quantile_model$residuals, "pnorm", mean(quantile_model$residuals), sd(quantile_model$residuals))
## Warning in ks.test.default(quantile_model$residuals, "pnorm",
## mean(quantile_model$residuals), : ties should not be present for the one-sample
## Kolmogorov-Smirnov test
## 
##  Asymptotic one-sample Kolmogorov-Smirnov test
## 
## data:  quantile_model$residuals
## D = 0.47739, p-value < 2.2e-16
## alternative hypothesis: two-sided
shapiro.test(quantile_model$residuals)
## 
##  Shapiro-Wilk normality test
## 
## data:  quantile_model$residuals
## W = 0.15938, p-value < 2.2e-16
# Pseudo R-squared calculation (as no adjusted R-squared is provided by rq)
pseudo_r_squared <- 1 - (quantile_model$rho / sum((data$Harga^2 - mean(data$Harga^2))^2))

# Residual Standard Error (RSE) approximation
n <- length(quantile_model$residuals)
p <- length(quantile_model$coefficients)
residuals <- quantile_model$residuals
rse <- sqrt(sum(residuals^2) / (n - p))

# Display results
cat("Pseudo R-squared: ", pseudo_r_squared, "\n")
## Pseudo R-squared:  0.9999978
cat("Residual Standard Error (RSE): ", rse, "\n")
## Residual Standard Error (RSE):  38968.26

Regresi

library(nlme)
## 
## Attaching package: 'nlme'
## The following object is masked from 'package:dplyr':
## 
##     collapse
library(lmtest)
library(lme4)
## Warning: package 'lme4' was built under R version 4.5.3
## Registered S3 method overwritten by 'lme4':
##   method           from
##   na.action.merMod car
## 
## Attaching package: 'lme4'
## The following object is masked from 'package:nlme':
## 
##     lmList
# Misalkan data sudah ada dalam data_clean
# Membuat model GLS dengan heteroskedastisitas dan autokorelasi
gls_model_ar1 <- glm(I(Harga^2) ~ `Kamar Tidur` + `Kamar Mandi` + `Luas Tanah`+`Luas Bangunan` + Garasi + Cicilan, data = data)

# Ringkasan model GLS
summary(gls_model_ar1)
## 
## Call:
## glm(formula = I(Harga^2) ~ `Kamar Tidur` + `Kamar Mandi` + `Luas Tanah` + 
##     `Luas Bangunan` + Garasi + Cicilan, data = data)
## 
## Coefficients:
##                  Estimate Std. Error t value Pr(>|t|)    
## (Intercept)      4301.444   3672.522   1.171  0.24177    
## `Kamar Tidur`     985.926   1404.692   0.702  0.48292    
## `Kamar Mandi`   -1774.895    964.316  -1.841  0.06598 .  
## `Luas Tanah`      -25.248      9.205  -2.743  0.00620 ** 
## `Luas Bangunan`    20.547      7.553   2.720  0.00663 ** 
## Garasi           4912.488   1229.822   3.994 6.96e-05 ***
## Cicilan            -3.298     26.912  -0.123  0.90249    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for gaussian family taken to be 1447544861)
## 
##     Null deviance: 1.4902e+12  on 1014  degrees of freedom
## Residual deviance: 1.4591e+12  on 1008  degrees of freedom
## AIC: 24299
## 
## Number of Fisher Scoring iterations: 2
# Uji Durbin-Watson
dwtest(gls_model_ar1)
## 
##  Durbin-Watson test
## 
## data:  gls_model_ar1
## DW = 1.9168, p-value = 0.08873
## alternative hypothesis: true autocorrelation is greater than 0
# Uji Kolmogorov-Smirnov untuk normalitas residual
residuals_gls <- residuals(gls_model_ar1)
ks.test(residuals_gls, "pnorm", mean=mean(residuals_gls), sd=sd(residuals_gls))
## Warning in ks.test.default(residuals_gls, "pnorm", mean = mean(residuals_gls),
## : ties should not be present for the one-sample Kolmogorov-Smirnov test
## 
##  Asymptotic one-sample Kolmogorov-Smirnov test
## 
## data:  residuals_gls
## D = 0.42272, p-value < 2.2e-16
## alternative hypothesis: two-sided
shapiro.test(residuals_gls)
## 
##  Shapiro-Wilk normality test
## 
## data:  residuals_gls
## W = 0.24236, p-value < 2.2e-16
# Uji Breusch-Pagan untuk heteroskedastisitas
bptest(gls_model_ar1)
## 
##  studentized Breusch-Pagan test
## 
## data:  gls_model_ar1
## BP = 8.0494, df = 6, p-value = 0.2345
# Menghitung Pseudo R-squared (McFadden's R-squared)
null_model <- glm(I(Harga^2) ~ 1, data = data)  # Model null tanpa prediktor
ll_full <- logLik(gls_model_ar1)  # Log likelihood dari model penuh
ll_null <- logLik(null_model)     # Log likelihood dari model null

pseudo_r_squared <- 1 - (as.numeric(ll_full) / as.numeric(ll_null))

# Menghitung Residual Standard Error (RSE)
n <- length(residuals_gls)
p <- length(coef(gls_model_ar1))
rss <- sum(residuals_gls^2)  # Residual sum of squares

rse <- sqrt(rss / (n - p))

# Menampilkan hasil
cat("Pseudo R-squared (McFadden): ", pseudo_r_squared, "\n")
## Pseudo R-squared (McFadden):  0.0008809646
cat("Residual Standard Error (RSE): ", rse, "\n")
## Residual Standard Error (RSE):  38046.61