Académique Documents
Professionnel Documents
Culture Documents
Khoirin Nisa
BAB 8
SIMULASI ANALISIS REGRESI
Jarak Emisi
31 553
38 590
48 608
52 682
63 752
67 725
75 834
84 752
89 845
99 960
Tabel 4. Data jarak & emisi
1. Input data
=====================================================
Jarak = c(31,38,48,52,63,67,75,84,89,99)
Emisi = c(553,590,608,682,752,725,834,752,845,960)
=====================================================
> regresi
Call:
lm(formula = Emisi ~ Jarak)
Coefficients:
(Intercept) Jarak
381.951 5.389
> uji_simultan
Response: Emisi
Df Sum Sq Mean Sq F value Pr(>F)
Jarak 1 131932 131932 74.757 2.486e-05 ***
Residuals 8 14118 1765
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
> uji_parsial
Call:
lm(formula = Emisi ~ Jarak)
Residuals:
Min 1Q Median 3Q Max
-82.653 -17.676 3.618 27.843 47.851
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 381.9506 42.4008 9.008 1.84e-05 ***
Jarak 5.3893 0.6233 8.646 2.49e-05 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
4. Uji linearitas
=====================================================
plot(Jarak,Emisi)
abline(lsfit(Jarak, Emisi))
=====================================================
Output :
5. Uji korelasi
=====================================================
korelasi = cor.test(Jarak,Emisi)
P.Value.korelasi = korelasi$p.value
=====================================================
Output :
> korelasi
> P.Value.korelasi
[1] 0.8861848
6. Uji Normalitas
=================================================
library(nortest) # Package uji kolmogorov
residual = uji_parsial$residuals
kolmogorov_tes = lillie.test(residual)
P.Value.kolmogorov = kolmogorov_tes$p
=================================================
Output :
> kolmogorov_tes
Lilliefors (Kolmogorov-Smirnov)
normality test
data: residual
D = 0.13276, p-value = 0.8862
> P.Value.kolmogorov
[1] 0.8861848
Output :
Untuk input data dengan format .txt maupun format excel (xlsx) saat kode
dijalankan, maka akan muncul jendela sebagai berikut :
Call:
lm(formula = Price ~ Calories + Protein + Fat, data =
data)
Coefficients:
(Intercept) Calories Protein Fat
0.015544 0.002654 0.053542 0.014683
Response: Price
Df Sum Sq Mean Sq F value Pr(>F)
Calories 1 1.35305 1.35305 17.0795 0.0008855 ***
Protein 1 1.97673 1.97673 24.9523 0.0001598 ***
Fat 1 0.09416 0.09416 1.1885 0.2928320
Residuals 15 1.18831 0.07922
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘
’ 1
> uji_parsial
Call:
lm(formula = Price ~ Calories + Protein + Fat, data = data)
Residuals:
Min 1Q Median 3Q Max
-0.47915 -0.10219 -0.07563 0.13604 0.61894
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 0.015544 0.467883 0.033 0.973936
Calories 0.002654 0.002102 1.263 0.226036
Protein 0.053542 0.012019 4.455 0.000463 ***
Fat 0.014683 0.013468 1.090 0.292832
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘
’ 1
4. Uji linearitas
=================================================
Price = data.contoh[,1]
Calories = data.contoh[,2]
Protein = data.contoh[,3]
Fat = data.contoh[,4]
par(mfrow=c(1,3))
#Price VS Calories
plot(Calories,Price)
abline(lsfit(Calories,Price))
#Price VS Protein
plot(Protein,Price)
abline(lsfit(Protein,Price))
#Price VS Fat
plot(Fat,Price)
abline(lsfit(Fat,Price))
=================================================
Output :
5. Uji korelasi
=================================================
korelasi = cor(data)
=================================================
Output :
> korelasi
6. Uji normalitas
=================================================================
library(nortest)
residual = uji_parsial$residuals
kolmogorov_tes = lillie.test(residual)
shapiro_test = shapiro.test(residual)
stat_uji_norm = rbind(kolmogorov_tes$statistic,
shapiro_test$statistic)
P_Value = rbind(kolmogorov_tes$p,shapiro_test$p)
Uji_Normalitas = cbind(stat_uji_norm,P_Value)
colnames(Uji_Normalitas)=c("Statistik Uji","P-Value")
rownames(Uji_Normalitas)=c("Kolmogorov-Smirnov","Shapiro-Wilk")
=================================================================
Output :
> Uji_Normalitas
7. Uji heteroskedastisitas
=================================================
library(lmtest)
Uji_Heteroskedastisitas = bptest(regresi)
=================================================
Output :
> Uji_Heteroskedastisitas
data: regresi
BP = 0.46259, df = 3, p-value = 0.927
8. Uji multikolinearitas
=================================================
library(car)
Uji_Multikolinearitas_1 = vif(regresi)
# Atau #
x = data[,2:4]
cor_data=cor(x)
VIF=diag(solve(cor_data))
Uji_Multikolinearitas_2 = VIF
=================================================
Output :
> Uji_Multikolinearitas_1
Calories Protein Fat
2.063865 1.543537 1.468350
> Uji_Multikolinearitas_2
Calories Protein Fat
2.063865 1.543537 1.468350
9. Uji autokorelasi
=================================================
library(car)
Uji_Autokorelasi = durbinWatsonTest(regresi)
=================================================
Output :
> Uji_Autokorelasi
lag Autocorrelation D-W Statistic p-value
1 -0.3597935 2.670475 0.216
Alternative hypothesis: rho != 0
Uji_Autokorelasi$dw
[1] 2.670475
Menggunakan grafik :
=================================================
n_row = nrow(data)
observation_order = seq(1:n_row)
plot(observation_order, residual)
lines(observation_order, residual)
=================================================
Output :
===================================================
beta0=beta1=1 #Menentukan b0 dan b1
galat = rnorm(100) #Membuat galat ~ N(0,1)
x = round(runif(100,10,50)) #Bangkit data x
y = beta0+beta1*x+galat #Bangkit data y
model = lm(y~x) #pendugaan model regresi dengan
metode kuadrat terkecil (lm=linear model)#
duga = model$coefficient #memanggil koefisien
regresi dugaan #
===================================================
===================================================
Ujinormal.beta = function(n,m)
{
Hasil = rep(0,m)
for (i in 1:m)
{
x = round(runif(n,10,50))
galat = rnorm(n)
y = x+galat
model = lm(y~x-1)
hasil[i] = model$coefficient
}
normal.test = shapiro.test(hasil)
pvalue = normal.test$p.value
return(pvalue)
}
sim1=Ujinormal.beta(10,100)
sim2=Ujinormal.beta(20,100)
sim3=Ujinormal.beta(30,100)
sim4=Ujinormal.beta(50,100)
sim5=Ujinormal.beta(100,100)
sim6=Ujinormal.beta(300,100)
sim7=Ujinormal.beta(500,100)
sim8=Ujinormal.beta(1000,100)
simulasi=rbind(sim1,sim2,sim3,sim4,sim5,sim6,sim7,s
im8)
size=c(10,20,30,50,100,300,500,1000)
hasil=data.frame(size,simulasi)
colnames(hasil)=c(”n”, ”p-value”)
hasil
===================================================
y = b0+b1*x1+b2*x2+galat
data1 = data.frame(x1,x2,y)
mod = lm(y~x1+x2, data=data1)
res = abs(resid(mod))
res = sort(res)
index = seq(1:100)
plot(index,res) #mengecek heteroskedastisitas
dengan plot galat
library(lmtest)
Uji_Heteroskedastisitas = bptest(regresi)
plot(regresi$fitted.values,regresi$residuals,main
= "Residual VS Fitted Values")
===================================================
plot(e)
x=rnorm(200)
y = 50 + 25*x + e
dataku = data.frame(x=x, y=y)
lm.model = lm(y~x, data=dataku)
anova(lm.model)
summary(lm.model)
residual = lm.model$residuals
plot(residual)
library(car)
Uji_Autokorelasi = durbinWatsonTest(regresi)
n_row = nrow(data)
observation_order = seq(1:n_row)
plot(observation_order, residual)
lines(observation_order, residual)
===================================================
𝑥𝑖 = √1 − 𝜌2 𝑥𝑖0 + 𝜌𝑥𝑝0
keterangan :
𝑥𝑖 : variabel bebas ke - 𝑖 yang berkorelasi dengan variabel 𝑥𝑝0
𝑥𝑖0 : variabel bebas ke - 𝑖 awal sebelum berkorelasi dengan variabel 𝑥𝑝0
x1 = (sqrt(1-((rho)^2)))*x1r+((rho)*x7r)
x2 = (sqrt(1-((rho)^2)))*x2r+((rho)*x7r)
x3 = (sqrt(1-((rho)^2)))*x3r+((rho)*x7r)
x4 = (sqrt(1-((rho)^2)))*x4r+((rho)*x7r)
x5 = (sqrt(1-((rho)^2)))*x5r+((rho)*x7r)
x6 = (sqrt(1-((rho)^2)))*x6r+((rho)*x7r)
X = cbind(x1,x2,x3,x4,x5,x6)
galat = rnorm(n)
b0=b1=b2=b3=b4=b5=b6=1
y = b0+b1*x1+b2*x2+b3*x3+b4*x4+b5*x5+b6*x6+galat
dataku = cbind(y,x1,x2,x3,x4,x5,x6)
data = data.frame(dataku)
data = round(data,digits=3)
return(data)
}
coba=dataku(50,0.99)
coba
data.X.simulasi = coba[,2:7]
corX = cor(data.X.simulasi)
VIF = (diag(solve(corX)))
===================================================
mod = lm(y~x)
anova(mod)
summary(mod)
residual = mod$residuals
library(nortest)
lilie.test(residual)
=================================================
bMMH = MMH$coefficients
bMMT = MMT$coefficients
bMMW = MMW$coefficients
B = cbind(bmkt,bMMH,bMMT,bMMW)
KOEF[i,] = B
}
MSE = colSums((KOEF-1)^2)/m
REKAP = cbind(j,t(MSE))
HASIL = rbind(HASIL,REKAP)
}
s = nrow(HASIL)
HASILAKHIR = HASIL[2:s,]
HASIL = data.frame(HASILAKHIR)
return(HASIL)
}
m = 100
pencilan1 = c(10,20,30)
n = 30
simn30 = SIMULASI(m,n,pencilan1)
hasil30 = cbind(n,simn30)
pencilan2 = c(5,10,15,20,25,30)
n = 200
simn200 = SIMULASI(m,n,pencilan2)
hasil200 = cbind(n,simn200)
HASIL = rbind(hasil30,hasil200)
HASIL = data.frame(HASIL)
colnames(HASIL) =
c("n","%pencilan","MSE(MKT)","MSE(HUBER)",
"MSE(TUKEY)", "MSE(WELSH)")
HASIL
=================================================