Vous êtes sur la page 1sur 9

AnggotaKelompok : Agung Budi Wirawan (16305141047)

RenitaSetyowati (16305141053)
Rohmadi Ahmad Ridwan (16305141057)
Diana Islamiyati (16305141070)
Laila BadriyatusZahro (16305144014)
Matematika E 2016
Analisis Regresi Terapan
Script :
#regresi polinomial kuadratik
library(readr)
LungCapData2=read.table("E:\\LungCapData.txt", header=T)
View(LungCapData2)
names(LungCapData2)
attach(LungCapData2)
summary(LungCapData2)
head(LungCapData2)
plot(Height, LungCap, main="Polynomial Regression")
model1<-lm(LungCap~Height,LungCapData2)
summary(model1)
abline(model1,lwd=3,col="red")

#cara 1
model2a<-lm(LungCap~Height+I(Height^2),LungCapData2)
summary(model2a)

#cara 2
HeightSquare<-(LungCapData2$Height)^2
model2b<-lm(LungCap~Height+HeightSquare,LungCapData2)
summary(model2b)

#cara 3
model2c<-lm(LungCap~poly(Height,degree=2,raw=T),LungCapData2)
#orthogonal polynomials raw=F
summary(model2c)

#perbandingan model dengan grafik


lines(smooth.spline(Height,predict(model2c)),col="blue",lwd=3)
#lwd untuk garis yang tebal
#lty untuk garis yang putus-putus

#perbandingan model dengan partial F-test


#H0: tidak ada perbedaan antara kedua model
#H1: model polinomial lebih baik
#H0 ditolak jika p-value kurang dari alpha
anova(model1,model2c)

model3<-lm(LungCap~Height+I(Height^2)+I(Height^3),LungCapData2)
summary(model3)
lines(smooth.spline(Height,predict(model3)),col="green",lwd=3,lt
y=3)
legend(46,15,legend=c("model1:linear","model2c:poly
x^2","model3:poly x^3"),
col=c("red","blue","green"),lty=c(1,1,3),lwd=3,bty="n",cex=0.9)

#regresi polinomial orde dua


HeightSquare<-(LungCapData2$Height)^2
AgeSquare<-(LungCapData2$Age)^2
HeightAge<-(LungCapData2$Height)*(LungCapData2$Age)
model4<-
lm(LungCap~Height+Age+HeightSquare+AgeSquare+HeightAge,LungCapDa
ta2)
summary(model4)
data.frame(coef(model4),confint(model4))

Output :

> #regresi polinomial kuadratik


> library(readr)
> LungCapData2=read.table("D:\\LungCapData(1).txt",header=T)
> View(LungCapData2)
> names(LungCapData2)
[1] "LungCap" "Age" "Height" "Smoke" "Gender" "Caesarean"
> attach(LungCapData2)
> summary(LungCapData2)
LungCap Age Height Smoke Gender Caes
arean
Min. : 0.507 Min. : 3.00 Min. :45.30 no :648 female:358 no :
561
1st Qu.: 6.150 1st Qu.: 9.00 1st Qu.:59.90 yes: 77 male :367 yes:
164
Median : 8.000 Median :13.00 Median :65.40
Mean : 7.863 Mean :12.33 Mean :64.84
3rd Qu.: 9.800 3rd Qu.:15.00 3rd Qu.:70.30
Max. :14.675 Max. :19.00 Max. :81.80
> head(LungCapData2)
LungCap Age Height Smoke Gender Caesarean
1 6.475 6 62.1 no male no
2 10.125 18 74.7 yes female no
3 9.550 16 69.7 no female yes
4 11.125 14 71.0 no male no
5 4.800 5 56.9 no male no
6 6.225 11 58.7 no female no
> plot(Height, LungCap, main="Polynomial Regression")
> model1<-lm(LungCap~Height,LungCapData2)
> summary(model1)

Call:
lm(formula = LungCap ~ Height, data = LungCapData2)

Residuals:
Min 1Q Median 3Q Max
-3.3619 -0.7014 -0.0032 0.7787 3.2938

Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -13.996829 0.367451 -38.09 <2e-16 ***
Height 0.337157 0.005633 59.86 <2e-16 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 1.092 on 723 degrees of freedom


Multiple R-squared: 0.8321, Adjusted R-squared: 0.8319
F-statistic: 3583 on 1 and 723 DF, p-value: < 2.2e-16

> abline(model1,lwd=3,col="red")

> #cara 1
> model2a<-lm(LungCap~Height+I(Height^2),LungCapData2)
> summary(model2a)

Call:
lm(formula = LungCap ~ Height + I(Height^2), data = LungCapData2)

Residuals:
Min 1Q Median 3Q Max
-3.4210 -0.6812 -0.0175 0.7805 3.3711

Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -1.875e+01 2.656e+00 -7.059 3.96e-12 ***
Height 4.876e-01 8.344e-02 5.843 7.75e-09 ***
I(Height^2) -1.175e-03 6.502e-04 -1.807 0.0712 .
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 1.09 on 722 degrees of freedom


Multiple R-squared: 0.8328, Adjusted R-squared: 0.8324
F-statistic: 1799 on 2 and 722 DF, p-value: < 2.2e-16

> #cara 2
> HeightSquare<-(LungCapData2$Height)^2
> model2b<-lm(LungCap~Height+HeightSquare,LungCapData2)
> summary(model2b)

Call:
lm(formula = LungCap ~ Height + HeightSquare, data = LungCapData2)

Residuals:
Min 1Q Median 3Q Max
-3.4210 -0.6812 -0.0175 0.7805 3.3711

Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -1.875e+01 2.656e+00 -7.059 3.96e-12 ***
Height 4.876e-01 8.344e-02 5.843 7.75e-09 ***
HeightSquare -1.175e-03 6.502e-04 -1.807 0.0712 .
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 1.09 on 722 degrees of freedom


Multiple R-squared: 0.8328, Adjusted R-squared: 0.8324
F-statistic: 1799 on 2 and 722 DF, p-value: < 2.2e-16
> #cara 3
> model2c<-lm(LungCap~poly(Height,degree=2,raw=T),LungCapData2) #orthogonal p
olynomials raw=F
> summary(model2c)

Call:
lm(formula = LungCap ~ poly(Height, degree = 2, raw = T), data = LungCapData2
)

Residuals:
Min 1Q Median 3Q Max
-3.4210 -0.6812 -0.0175 0.7805 3.3711

Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -1.875e+01 2.656e+00 -7.059 3.96e-12 ***
poly(Height, degree = 2, raw = T)1 4.876e-01 8.344e-02 5.843 7.75e-09 ***
poly(Height, degree = 2, raw = T)2 -1.175e-03 6.502e-04 -1.807 0.0712 .
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 1.09 on 722 degrees of freedom


Multiple R-squared: 0.8328, Adjusted R-squared: 0.8324
F-statistic: 1799 on 2 and 722 DF, p-value: < 2.2e-16

> #perbandingan model dengan grafik


> lines(smooth.spline(Height,predict(model2c)),col="blue",lwd=3)
> #lwd untuk garis yang tebal
> #lty untuk garis yang putus-putus
>
> #perbandingan model dengan partial F-test
> #H0: tidak ada perbedaan antara kedua model
> #H1: model polinomial lebih baik
> #H0 ditolak jika p-value kurang dari alpha
> anova(model1,model2c)
Analysis of Variance Table

Model 1: LungCap ~ Height


Model 2: LungCap ~ poly(Height, degree = 2, raw = T)
Res.Df RSS Df Sum of Sq F Pr(>F)
1 723 861.48
2 722 857.60 1 3.8768 3.2638 0.07124 .
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

> model3<-lm(LungCap~Height+I(Height^2)+I(Height^3),LungCapData2)
> summary(model3)

Call:
lm(formula = LungCap ~ Height + I(Height^2) + I(Height^3), data = LungCapData
2)

Residuals:
Min 1Q Median 3Q Max
-3.4258 -0.6834 -0.0120 0.7801 3.3719

Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -1.180e+01 1.806e+01 -0.653 0.514
Height 1.531e-01 8.639e-01 0.177 0.859
I(Height^2) 4.132e-03 1.366e-02 0.303 0.762
I(Height^3) -2.777e-05 7.140e-05 -0.389 0.697

Residual standard error: 1.091 on 721 degrees of freedom


Multiple R-squared: 0.8329, Adjusted R-squared: 0.8322
F-statistic: 1198 on 3 and 721 DF, p-value: < 2.2e-16

> lines(smooth.spline(Height,predict(model3)),col="green",lwd=3,lty=3)
> legend(46,15,legend=c("model1:linear","model2c:poly x^2","model3:poly x^3")
, col=c("red","blue","green"),lty=c(1,1,3),lwd=3,bty="n",cex=0.9)
>
> #regresi polinomial orde dua
> HeightSquare<-(LungCapData2$Height)^2
> AgeSquare<-(LungCapData2$Age)^2
> HeightAge<-(LungCapData2$Height)*(LungCapData2$Age)
> model4<-lm(LungCap~Height+Age+HeightSquare+AgeSquare+HeightAge,LungCapData2
)
> summary(model4)

Call:
lm(formula = LungCap ~ Height + Age + HeightSquare + AgeSquare +
HeightAge, data = LungCapData2)

Residuals:
Min 1Q Median 3Q Max
-3.4814 -0.6907 -0.0120 0.7206 3.2006

Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -18.145561 4.217388 -4.303 1.92e-05 ***
Height 0.528582 0.173625 3.044 0.00242 **
Age -0.134723 0.273887 -0.492 0.62295
HeightSquare -0.002528 0.001851 -1.365 0.17254
AgeSquare -0.006023 0.006361 -0.947 0.34400
HeightAge 0.006278 0.006262 1.003 0.31643
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 1.056 on 719 degrees of freedom


Multiple R-squared: 0.8436, Adjusted R-squared: 0.8425
F-statistic: 775.9 on 5 and 719 DF, p-value: < 2.2e-16

Interpretasi
Dari cara 1 sampai cara 3, dapat ditarik kesimpulan :
Karena p-value = 2.2e-16 maka H0 ditolak. Jadi dengan taraf nyata 0.05 dapat disimpulkan
bahwa terdapat perbedaan antara model tersebut.

> data.frame(coef(model4),confint(model4))
coef.model4. X2.5.. X97.5..
(Intercept) -18.145560596 -26.425427088 -9.865694104
Height 0.528581953 0.187709052 0.869454854
Age -0.134722648 -0.672436902 0.402991606
HeightSquare -0.002528113 -0.006163053 0.001106827
AgeSquare -0.006023500 -0.018512084 0.006465085
HeightAge 0.006277631 -0.006016063 0.018571324

Gambar 1

Gambar 2
Gambar 3

Gambar 4
Gambar 5

Vous aimerez peut-être aussi