0% found this document useful (0 votes)
6 views8 pages

R Version 4

The document details the use of R version 4.2.3 for statistical analysis, including loading libraries, reading data, and performing linear regression modeling. It presents the results of a regression analysis with coefficients, residuals, and various statistical tests such as the Breusch-Pagan test and Moran's I test for spatial autocorrelation. Additionally, it includes calculations of standard deviations and residuals, as well as importing a matrix for further analysis.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views8 pages

R Version 4

The document details the use of R version 4.2.3 for statistical analysis, including loading libraries, reading data, and performing linear regression modeling. It presents the results of a regression analysis with coefficients, residuals, and various statistical tests such as the Breusch-Pagan test and Moran's I test for spatial autocorrelation. Additionally, it includes calculations of standard deviations and residuals, as well as importing a matrix for further analysis.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

R version 4.2.

3 (2023-03-15 ucrt) -- "Shortstop Beagle"


Copyright (C) 2023 The R Foundation for Statistical Computing
Platform: x86_64-w64-mingw32/x64 (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.


You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

Natural language support but running in an English locale

R is a collaborative project with many contributors.


Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or


'[Link]()' for an HTML browser interface to help.
Type 'q()' to quit R.

[Workspace loaded from ~/.RData]

Loading required package: sp


> library(tmap)

Attaching package: ‘tmap’

The following object is masked from ‘package:datasets’:

rivers

> library(raster)
> library(sf)
Linking to GEOS 3.9.3, GDAL 3.5.2, PROJ 8.2.1; sf_use_s2() is TRUE
> library(ggplot2)
> library(sf)
> library(sp)
> library(spdep)
Loading required package: spData
To access larger datasets in this package, install the
spDataLarge package with: `[Link]('spDataLarge',
repos='[Link] type='source')`
> library(car)
Loading required package: carData
> library(stats)
> library(zoo)

Attaching package: ‘zoo’

The following objects are masked from ‘package:base’:

[Link], [Link]

> library(lmtest)
> library(spatialreg)
Loading required package: Matrix

Attaching package: ‘spatialreg’

The following objects are masked from ‘package:spdep’:

[Link], [Link], [Link],


[Link], [Link],
[Link], [Link], [Link],
[Link], [Link]

> data=[Link]([Link](),header=T)
> data
Y X1 X2 X3 X4 X5
1 6.03 64.810 3413666 71.70 14.45 9.55
2 5.89 71.105 2710494 68.67 8.15 9.82
3 5.94 69.575 2742476 69.18 5.95 9.28
4 4.23 64.020 3191663 64.81 6.68 9.32
5 4.53 68.475 2943033 60.60 7.58 8.81
6 4.11 70.465 3404177 61.24 11.78 8.50
7 3.42 70.605 2418280 67.09 14.04 9.03
8 4.23 69.655 2633285 62.84 11.11 8.29
9 4.56 67.845 3498479 60.72 4.52 8.25
10 6.80 68.435 3279194 74.11 5.69 10.41
11 6.53 65.185 4901798 60.81 4.44 11.45
12 7.44 66.305 1986670 59.01 7.62 8.83
13 5.13 71.720 1958170 61.46 10.77 8.01
14 3.69 74.255 1981782 76.37 11.04 9.83
15 4.88 72.680 2040244 62.59 10.35 8.11
16 7.52 64.160 2661280 60.42 6.17 9.15
17 2.69 77.090 2713672 75.60 4.25 9.45
18 2.80 73.360 2371407 68.04 13.85 7.74
19 3.14 75.775 2123994 58.15 19.96 7.82
20 5.05 68.980 2608602 54.76 6.71 7.71
21 4.10 66.490 3181013 55.63 5.11 8.73
22 4.31 69.635 3149978 59.83 4.29 8.55
23 5.31 64.840 3201396 69.89 6.11 9.99
24 4.01 69.390 3251703 66.70 6.45 9.34
25 6.10 63.730 3485000 63.86 7.38 9.77
26 2.95 69.410 2599546 66.18 12.41 8.96
27 4.33 65.920 3385145 60.57 8.70 8.76
28 3.15 69.895 2758985 64.00 11.43 9.31
29 3.06 70.705 2989350 60.18 15.15 8.10
30 2.27 70.840 2871795 61.17 11.49 8.13
31 6.31 63.510 2812828 66.16 16.42 10.20
32 4.31 67.305 2976720 65.75 6.46 9.26
33 5.38 66.515 3282000 65.92 20.49 7.93
34 2.67 76.455 3864696 48.32 26.03 7.15
> summary(data)
Y X1 X2 X3
Min. :2.270 Min. :63.51 Min. :1958170 Min. :48.32
1st Qu.:3.487 1st Qu.:66.35 1st Qu.:2614772 1st Qu.:60.58
Median :4.320 Median :69.40 Median :2907414 Median :63.35
Mean :4.614 Mean :69.09 Mean :2923309 Mean :63.89
3rd Qu.:5.763 3rd Qu.:70.81 3rd Qu.:3272321 3rd Qu.:66.99
Max. :7.520 Max. :77.09 Max. :4901798 Max. :76.37
X4 X5
Min. : 4.250 Min. : 7.150
1st Qu.: 6.240 1st Qu.: 8.160
Median : 8.425 Median : 8.895
Mean :10.089 Mean : 8.928
3rd Qu.:12.252 3rd Qu.: 9.422
Max. :26.030 Max. :11.450
> #standar deviasi
> sd(data$Y)
[1] 1.419065
> sd(data$X1)
[1] 3.657757
> sd(data$X2)
[1] 602114.2
> sd(data$X3)
[1] 5.920962
> sd(data$X4)
[1] 5.183509
> sd(data$X5)
[1] 0.9119113
> #PEMODELAN REGRESI LINIER BERGANDA
> Reg=lm(formula=Y~X1+X2+X3+X4+X5,data=data)
> summary(Reg)

Call:
lm(formula = Y ~ X1 + X2 + X3 + X4 + X5, data = data)

Residuals:
Min 1Q Median 3Q Max
-1.70618 -0.66107 -0.04784 0.78589 1.73809

Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 1.839e+01 5.512e+00 3.336 0.00241 **
X1 -2.147e-01 6.291e-02 -3.412 0.00198 **
X2 -6.481e-07 3.817e-07 -1.698 0.10057
X3 -4.836e-02 4.571e-02 -1.058 0.29914
X4 -4.617e-03 4.153e-02 -0.111 0.91227
X5 6.823e-01 3.549e-01 1.923 0.06475 .
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 1.056 on 28 degrees of freedom


Multiple R-squared: 0.5299, Adjusted R-squared: 0.4459
F-statistic: 6.312 on 5 and 28 DF, p-value: 0.0004853

> #ASUMSI REGRESI LINIER BERGANDA


> vif(Reg)
X1 X2 X3 X4 X5
1.566335 1.561951 2.166809 1.370755 3.097661
> bptest(Reg,varformula=~[Link](Reg),studentize=FALSE)

Breusch-Pagan test
data: Reg
BP = 0.78967, df = 1, p-value = 0.3742

> abs_residuals <- abs(residuals(Reg))


> dwtest(Reg,alternative='[Link]')

Durbin-Watson test

data: Reg
DW = 1.3748, p-value = 0.04451
alternative hypothesis: true autocorrelation is not 0

> [Link](resid(Reg))

Shapiro-Wilk normality test

data: resid(Reg)
W = 0.96631, p-value = 0.3675

> bptest_result <- bptest(Reg, varformula=~[Link](Reg),


studentize=FALSE)
> print(bptest_result)

Breusch-Pagan test

data: Reg
BP = 0.78967, df = 1, p-value = 0.3742

>
> #memanggil nilai error dari rlb
> Reg$residuals
1 2 3 4 5
0.787745480 1.183551155 1.308784341 -1.537872266 -0.294087099
6 7 8 9 10
0.273848852 -1.093349473 -0.062086622 0.334427574 1.738093933
11 12 13 14 15
0.463442137 1.439891657 0.966416977 -0.433609918 0.960173278
16 17 18 19 20
1.339779519 -0.159972648 -0.227038704 -0.033599516 0.581692359
21 22 23 24 25
-1.193149866 -0.005967254 -0.489649210 -0.489462138 0.110230661
26 27 28 29 30
-1.706178730 -0.718211411 -1.647490622 -0.756252240 -1.582952132
31 32 33 34
-0.303063878 -0.806587262 1.272163212 0.780339852
> res=Reg$residuals
> resv=[Link](res)
> [Link](1)
>
> # Membulatkan setiap nilai residual ke 3 angka di belakang koma
> rounded_res<- round(res, 3)
> rounded_res
1 2 3 4 5 6 7 8 9
10
0.788 1.184 1.309 -1.538 -0.294 0.274 -1.093 -0.062 0.334
1.738
11 12 13 14 15 16 17 18 19
20
0.463 1.440 0.966 -0.434 0.960 1.340 -0.160 -0.227 -0.034
0.582
21 22 23 24 25 26 27 28 29
30
-1.193 -0.006 -0.490 -0.489 0.110 -1.706 -0.718 -1.647 -0.756 -
1.583
31 32 33 34
-0.303 -0.807 1.272 0.780
> mean_rounded_res <- mean(rounded_res)
> mean_rounded_res
[1] -1.956347e-18
>
>
> #IMPOR MATRIKS
> bobot=[Link]("D:/DRAFT SEMHAS/DATA/[Link]",header =
FALSE)
> bot<-[Link](bobot)
> wbot <- mat2listw(bot, style = "W")
>
> #PENGUJIAN Indeks Morans Error RLB
> [Link](resv,nb2listw(wbot$neighbours,style =
"W"),alternative = "[Link]")

Moran I test under randomisation

data: resv
weights: nb2listw(wbot$neighbours, style = "W")

Moran I statistic standard deviate = 2.4223, p-value = 0.01542


alternative hypothesis: [Link]
sample estimates:
Moran I statistic Expectation Variance
0.38054090 -0.03030303 0.02876840

> #PENGUJIAN Indeks Morans X1


> variable <- data$X1
> moran_test_X1 <- [Link](variable, listw = wbot, alternative
= "[Link]")
> print(moran_test_X1)

Moran I test under randomisation

data: variable
weights: wbot

Moran I statistic standard deviate = 1.7721, p-value = 0.07638


alternative hypothesis: [Link]
sample estimates:
Moran I statistic Expectation Variance
0.26789716 -0.03030303 0.02831705
> #PENGUJIAN Indeks Morans X2
> variable <- data$X2
> moran_test_X2 <- [Link](variable, listw = wbot, alternative
= "[Link]")
> print(moran_test_X2)

Moran I test under randomisation

data: variable
weights: wbot

Moran I statistic standard deviate = 1.4926, p-value = 0.1355


alternative hypothesis: [Link]
sample estimates:
Moran I statistic Expectation Variance
0.21114067 -0.03030303 0.02616604

> #PENGUJIAN Indeks Morans X3


> variable <- data$X3
> moran_test_X3 <- [Link](variable, listw = wbot, alternative
= "[Link]")
> print(moran_test_X3)

Moran I test under randomisation

data: variable
weights: wbot

Moran I statistic standard deviate = 0.16274, p-value = 0.8707


alternative hypothesis: [Link]
sample estimates:
Moran I statistic Expectation Variance
-0.003322141 -0.030303030 0.027487688

> #PENGUJIAN Indeks Morans X4


> variable <- data$X4
> moran_test_X4 <- [Link](variable, listw = wbot, alternative
= "[Link]")
> print(moran_test_X4)

Moran I test under randomisation

data: variable
weights: wbot

Moran I statistic standard deviate = 3.4693, p-value =


0.0005219
alternative hypothesis: [Link]
sample estimates:
Moran I statistic Expectation Variance
0.53752032 -0.03030303 0.02678887

> #PENGUJIAN Indeks Morans X5


> variable <- data$X5
> moran_test_X5 <- [Link](variable, listw = wbot, alternative
= "[Link]")
> print(moran_test_X5)

Moran I test under randomisation

data: variable
weights: wbot

Moran I statistic standard deviate = 1.1861, p-value = 0.2356


alternative hypothesis: [Link]
sample estimates:
Moran I statistic Expectation Variance
0.16711031 -0.03030303 0.02770106

>
> # Uji LM dengan [Link]
> LM <- [Link](Reg, nb2listw(wbot$neighbours), test =
c("LMerr", "LMlag"))
> LM

Rao's score (a.k.a Lagrange multiplier) diagnostics for


spatial dependence

data:
model: lm(formula = Y ~ X1 + X2 + X3 + X4 + X5, data = data)
test weights: nb2listw(wbot$neighbours)

RSerr = 4.8293, df = 1, p-value = 0.02798

Rao's score (a.k.a Lagrange multiplier) diagnostics for


spatial dependence

data:
model: lm(formula = Y ~ X1 + X2 + X3 + X4 + X5, data = data)
test weights: nb2listw(wbot$neighbours)

RSlag = 2.4769, df = 1, p-value = 0.1155

>
> #PEMODELAN SDEM
> sdem_model <- errorsarlm(Y ~ X1 + X2 + X3 + X4 + X5 , data =
data, listw = wbot)
> summary(sdem_model)

Call:errorsarlm(formula = Y ~ X1 + X2 + X3 + X4 + X5, data = data,


listw = wbot)

Residuals:
Min 1Q Median 3Q Max
-1.909894 -0.429778 0.025441 0.646802 1.914121

Type: error
Coefficients: (asymptotic standard errors)
Estimate Std. Error z value Pr(>|z|)
(Intercept) 1.9432e+01 4.2002e+00 4.6264 3.720e-06
X1 -2.2205e-01 4.6701e-02 -4.7547 1.988e-06
X2 -7.6916e-07 3.3400e-07 -2.3029 0.02128
X3 -4.4036e-02 3.7065e-02 -1.1881 0.23481
X4 -1.4902e-02 3.7149e-02 -0.4011 0.68831
X5 6.4411e-01 2.8254e-01 2.2797 0.02263

Lambda: 0.3478, LR test value: 4.9134, p-value: 0.02665


Asymptotic standard error: 0.1444
z-value: 2.4085, p-value: 0.016017
Wald statistic: 5.801, p-value: 0.016017

Log likelihood: -44.34806 for error model


ML residual variance (sigma squared): 0.74769, (sigma: 0.86469)
Number of observations: 34
Number of parameters estimated: 8
AIC: 104.7, (AIC for lm: 107.61)

>
> sdem_model <- errorsarlm(Y ~ X1 + X2 + X3 + X4 + X5, data =
data, listw = wbot, etype = "mixed")
Error in [Link](-(mat), [Link] = [Link]) :
system is computationally singular: reciprocal condition number
= 2.26011e-18
In addition: Warning message:
In errorsarlm(Y ~ X1 + X2 + X3 + X4 + X5, data = data, listw =
wbot, :
inversion of asymptotic covariance matrix failed for [Link] =
2.22044604925031e-16
reciprocal condition number = 4.43158e-18 - using numerical
Hessian.

You might also like