Sunteți pe pagina 1din 12

Metode Forward

Penyelesaian dengan Metode Forward Selection menggunakan Software R


Input :
library(readxl)

dataset=read_excel("F:/berat badan.xlsx")

View(dataset)

linearmodel=lm(Y~,data=dataset)

summary(linearmodel)

library(MASS)

stepAIC(linearmodel,direction="forward")

Output :

Syntax:
> library(readxl)
> dataset=read_excel("F:/berat badan.xlsx")
> dataset
# A tibble: 30 x 5
Y X1 X2 X3 X4
<dbl> <dbl> <dbl> <dbl> <dbl>
1 63.5 38.9 45.4 30.4 148
2 62.6 39 44.5 35.9 150
3 63.7 22.5 58.9 19.5 167
4 57.5 35.6 37.7 35 148
5 49.9 28 61.3 19.5 158
6 61.3 43.5 44 18.4 148
7 45.6 30 61.6 17.5 158
8 57 40.9 43.2 25.5 140
9 49.8 20.1 60.7 39.1 155
10 73.6 23.5 56 53.4 165
# ... with 20 more rows
> linearmodel=lm(Y~,data=dataset)
Error: unexpected ',' in "linearmodel=lm(Y~,"
> linearmodel=lm(Y~.,data=dataset)
> linearmodel
Call:
lm(formula = Y ~ ., data = dataset)

Coefficients:
(Intercept) X1 X2 X3 X4
-122.37729 0.16537 -0.97900 0.02346 1.47430

> summary(linearmodel)

Call:
lm(formula = Y ~ ., data = dataset)

Residuals:
Min 1Q Median 3Q Max
-15.8162 -5.2968 0.5184 4.9250 12.7717

Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -122.37729 44.29393 -2.763 0.0106 *
X1 0.16537 0.35233 0.469 0.6429
X2 -0.97900 0.40102 -2.441 0.0221 *
X3 0.02346 0.13526 0.173 0.8637
X4 1.47430 0.26294 5.607 7.82e-06 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 8.18 on 25 degrees of freedom


Multiple R-squared: 0.6547, Adjusted R-squared: 0.5995
F-statistic: 11.85 on 4 and 25 DF, p-value: 1.548e-05

> library(MASS)
> stepAIC(linearmodel,direction="forward")
Start: AIC=130.63
Y ~ X1 + X2 + X3 + X4

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

Coefficients:
(Intercept) X1 X2 X3 X4
-122.37729 0.16537 -0.97900 0.02346 1.47430

Penyelesaian dengan Metode Forward Selection menggunakan Software SPSS

Membuka data pada Microsoft Excell → blok data yang akan di copy →
Klik Copy.
Copy data pada software SPSS.
Klik Variable View dan ganti Name dengan Y,X1,X2,X3,X4.

Klik Measure → pilih Scale.

klik Analyze → pilih Regression → pilih Linear.


Kemudian, akan muncul kotak Linear Regression.
Klik Y untuk mengisi kotak Dependent
Klik X1,X2,X3,X4 untuk mengisi kotak Independent(s)

Klik OK

Kemudian, akan muncul output seperti berikut.


Berikut Output dari Software SPSS

Regression

Variables Entered/Removeda

Variables Variables
Model Entered Removed Method
1 X4 . Forward
(Criterion:
Probability
-of-F-to-
enter <= .
Variables Entered/Removeda
050)
2 Forward
(Criterion:
Probability
X2 .
-of-F-to-
enter <= .
050)
Dependent Variable: Y

Model Summaryc
Change Statistics
Adjusted Std. Error of the R Square Sig. F
Model R R Square R Square Estimate Change F Change df1 df2 Change
1 .599a .359 .336 10.53464 .359 15.662 1 28 .000
2 .807b .651 .626 7.90947 .293 22.671 1 27 .000
a. Predictors: (Constant), X4
b. Predictors: (Constant), X4, X2
c. Dependent Variable: Y

ANOVAa
Model Sum of Squares df Mean Square F Sig.
1 Regression 1738.134 1 1738.134 15.662 .000b
Residual 3107.400 28 110.979
Total 4845.534 29
2 Regression 3156.420 2 1578.210 25.227 .000c
Residual 1689.114 27 62.560
Total 4845.534 29
a. Dependent Variable: Y
b. Predictors: (Constant), X4
c. Predictors: (Constant), X4, X2

Coefficientsa
Standardized
Unstandardized Coefficients Coefficients
Model B Std. Error Beta t Sig.
1 (Constant) -85.896 37.743 -2.276 .031
X4 .941 .238 .599 3.958 .000
2 (Constant) -110.538 28.806 -3.837 .001
X4 1.488 .212 .947 7.009 .000
X2 -1.134 .238 -.643 -4.761 .000
a. Dependent Variable: Y
Excluded Variablesa
Collinearity
Statistics
Model Beta In t Sig. Partial Correlation Tolerance
1 X1 .525b 3.577 .001 .567 .748
X2 -.643b -4.761 .000 -.676 .707
X3 .192b 1.107 .278 .208 .755
2 X1 .099c .469 .643 .092 .297
X3 .021c .148 .884 .029 .697
a. Dependent Variable: Y
b. Predictors in the Model: (Constant), X4
c. Predictors in the Model: (Constant), X4, X2

Kesimpulan :
Dari metode Forward Selection didapat model terbaik sebagai berikut :

Y = -110.538 – 1.134 X2 + 1.488X4 + e

Interpretasi :

Rata-rata berat badan reponden pengkonsumsi HERBALIFE Nutrition adalah turun sebesar
110.538 kilogram jika diasumsikan semua variabel lainnya adalah konstan.

Jika nilai kadar air dalam tubuh meningkat sebesar 1 satuan dan diasumsikan variabel
lainnya konstan, maka berat badan reponden pengkonsumsi HERBALIFE Nutrition akan
menurun sebesar 1.134 kilogram

Jika tinggi badan meningkat sebesar 1 cm dan diasumsikan variabel lainnya konstan, maka
berat badan reponden pengkonsumsi HERBALIFE Nutrition akan meningkat sebesar 1.488
kilogram

Metode Stepwise

Input :

> data=read.csv (file.choose(), sep=",",header=T)

> data
> FitAll <- lm(y~.,data=data)

> FitStart <- lm(y~.,data=data)

> summary(FitStart)

> step(FitStart,direction="both",scope=formula(FitAll))

Output :

> data=read.csv (file.choose(),sep=",",header=T)

> data

Y X1 X2 X3 X4

1 63.5 38.9 45.40 30.4 148

2 62.6 39.0 44.50 35.9 150

3 63.7 22.5 58.90 19.5 167

4 57.5 35.6 37.70 35.0 148

5 49.9 28.0 61.30 19.5 158

6 61.3 43.5 44.00 18.4 148

7 45.6 30.0 61.60 17.5 158

8 57.0 40.9 43.25 25.5 140

9 49.8 20.1 60.70 39.1 155

10 73.6 23.5 56.00 53.4 165

11 81.3 43.6 45.45 43.0 160

12 65.7 20.1 62.10 51.0 167

13 35.3 16.4 61.20 28.0 148

14 60.6 39.6 44.50 34.0 150

15 61.4 38.0 44.20 35.0 151

16 51.8 28.4 53.90 36.0 159


17 35.4 20.4 62.10 28.6 151

18 40.0 29.0 62.00 32.0 155

19 70.0 28.4 50.90 52.1 171

20 81.3 30.8 55.70 58.6 170

21 78.5 26.4 60.00 57.0 169

22 76.9 25.3 57.20 34.5 168

23 71.0 22.0 59.80 31.2 165

24 70.5 25.2 59.20 20.5 159

25 75.3 20.3 58.30 20.1 165

26 69.9 19.1 54.80 22.5 158

27 73.4 30.4 58.20 23.1 159

28 71.5 22.5 59.70 55.4 160

29 73.4 26.2 58.20 55.4 163

30 70.6 22.1 59.50 55.4 169

> FitAll <- lm(Y~.,data=data)

> FitStart <- lm(Y~.,data=data)

> summary(FitStart)

Call:

lm(formula = Y ~ ., data = data)

Residuals:

Min 1Q Median 3Q Max

-15.8162 -5.2968 0.5184 4.9250 12.7717


Coefficients:

Estimate Std. Error t value Pr(>|t|)

(Intercept) -122.37729 44.29393 -2.763 0.0106 *

X1 0.16537 0.35233 0.469 0.6429

X2 -0.97900 0.40102 -2.441 0.0221 *

X3 0.02346 0.13526 0.173 0.8637

X4 1.47430 0.26294 5.607 7.82e-06 ***

---

Signif. codes:

0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 8.18 on 25 degrees of freedom

Multiple R-squared: 0.6547, Adjusted R-squared: 0.5995

F-statistic: 11.85 on 4 and 25 DF, p-value: 1.548e-05

> step(FitStart,direction="both",scope=formula(FitAll))

Start: AIC=130.63

Y ~ X1 + X2 + X3 + X4

Df Sum of Sq RSS AIC

- X3 1 2.01 1675.0 128.67

- X1 1 14.74 1687.7 128.90

<none> 1673.0 130.63

- X2 1 398.82 2071.8 135.05

- X4 1 2103.78 3776.7 153.06


Step: AIC=128.67

Y ~ X1 + X2 + X4

Df Sum of Sq RSS AIC

- X1 1 14.15 1689.1 126.92

<none> 1675.0 128.67

+ X3 1 2.01 1673.0 130.63

- X2 1 433.21 2108.2 133.57

- X4 1 3082.41 4757.4 157.99

Step: AIC=126.92

Y ~ X2 + X4

Df Sum of Sq RSS AIC

<none> 1689.1 126.92

+ X1 1 14.15 1675.0 128.67

+ X3 1 1.42 1687.7 128.90

- X2 1 1418.29 3107.4 143.21

- X4 1 3073.12 4762.2 156.02

Call:

lm(formula = Y ~ X2 + X4, data = data)

Coefficients:
(Intercept) X2 X4

-110.538 -1.134 1.488

Dari metode stepwise diperoleh model terbaik sebagai berikut :

Y = -110.538 – 1.134X2 + 1.488X4


Interpretasi :

Jika semua variabel lainnya dianggap konstan, maka berat badan seseorang pengkonsumsi
HERBALIFE Nutrition adalah -110.538 kg.

Jika kadar air meningkat sebesar 1% dan variabel lainnya dianggap konstan, maka berat badan
akan menurun sebesar 1.134 kg.

Jika tinggi badan meningkat sebesar 1 cm satuan dan variabel lainnya dianggap konstan, maka
berat badan akan meningkat sebesar 1.488 kg.

S-ar putea să vă placă și