Latihan penggunaan RMarkdown menggunakan built-in dataset yang ada di R, yaitu data swiss. Data tersebut berisi standarisasi ukuran kesuburan dan indikator sosial ekonomi untuk masing-masing 47 provinsi berbahasa Perancis di Swiss pada sekitar tahun 1888.
Swiss, pada tahun 1888, memasuki periode yang dikenal sebagai transisi demografi; yaitu, kesuburannya mulai turun dari tingkat tinggi yang biasa terjadi di negara-negara terbelakang. Data yang dikumpulkan adalah untuk 47 “provinsi” berbahasa Perancis pada sekitar tahun 1888.
summary(swiss)
## Fertility Agriculture Examination Education
## Min. :35.00 Min. : 1.20 Min. : 3.00 Min. : 1.00
## 1st Qu.:64.70 1st Qu.:35.90 1st Qu.:12.00 1st Qu.: 6.00
## Median :70.40 Median :54.10 Median :16.00 Median : 8.00
## Mean :70.14 Mean :50.66 Mean :16.49 Mean :10.98
## 3rd Qu.:78.45 3rd Qu.:67.65 3rd Qu.:22.00 3rd Qu.:12.00
## Max. :92.50 Max. :89.70 Max. :37.00 Max. :53.00
## Catholic Infant.Mortality
## Min. : 2.150 Min. :10.80
## 1st Qu.: 5.195 1st Qu.:18.15
## Median : 15.140 Median :20.00
## Mean : 41.144 Mean :19.94
## 3rd Qu.: 93.125 3rd Qu.:21.70
## Max. :100.000 Max. :26.60
plot(swiss$Fertility)
plot(swiss$Infant.Mortality)
library(corrplot)
## corrplot 0.92 loaded
#Correlation between fertility and infant mortality
cor.test(swiss$Fertility, swiss$Infant.Mortality)
##
## Pearson's product-moment correlation
##
## data: swiss$Fertility and swiss$Infant.Mortality
## t = 3.0737, df = 45, p-value = 0.003585
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
## 0.1469699 0.6285366
## sample estimates:
## cor
## 0.416556
#Correlation between agriculture and fertility
cor.test(swiss$Agriculture, swiss$Fertility)
##
## Pearson's product-moment correlation
##
## data: swiss$Agriculture and swiss$Fertility
## t = 2.5316, df = 45, p-value = 0.01492
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
## 0.07334947 0.58130587
## sample estimates:
## cor
## 0.3530792
#plot
plot(swiss$Fertility, swiss$Infant.Mortality)
plot(swiss$Agriculture, swiss$Fertility)
#ggplot
library(ggplot2)
ggplot(swiss, aes(x=Fertility, y=Infant.Mortality))+
geom_line(color="indianred3",
linewidth=0.7)+
geom_smooth()+
labs(title="Infant Mortality by Fertility",
x="Infant Mortality",
y="Fertility")
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
ggplot(swiss, aes(x=Agriculture, y=Fertility))+
geom_line(color="indianred3",
linewidth=0.7)+
geom_smooth()+
labs(title="Fertility by Agriculture",
x="Fertility",
y="Agriculture")
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
#GGCorrPlot
library(ggcorrplot)
df <- dplyr::select_if(swiss, is.numeric)
r <- cor(df, use="complete.obs")
round(r,2)
## Fertility Agriculture Examination Education Catholic
## Fertility 1.00 0.35 -0.65 -0.66 0.46
## Agriculture 0.35 1.00 -0.69 -0.64 0.40
## Examination -0.65 -0.69 1.00 0.70 -0.57
## Education -0.66 -0.64 0.70 1.00 -0.15
## Catholic 0.46 0.40 -0.57 -0.15 1.00
## Infant.Mortality 0.42 -0.06 -0.11 -0.10 0.18
## Infant.Mortality
## Fertility 0.42
## Agriculture -0.06
## Examination -0.11
## Education -0.10
## Catholic 0.18
## Infant.Mortality 1.00
ggcorrplot(r,
hc.order= TRUE,
type="lower",
lab= TRUE)
#Linear Model
fertility_lm <- lm(Fertility~Agriculture + Examination + Education + Catholic + Infant.Mortality,data=swiss)
summary(fertility_lm)
##
## Call:
## lm(formula = Fertility ~ Agriculture + Examination + Education +
## Catholic + Infant.Mortality, data = swiss)
##
## Residuals:
## Min 1Q Median 3Q Max
## -15.2743 -5.2617 0.5032 4.1198 15.3213
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 66.91518 10.70604 6.250 1.91e-07 ***
## Agriculture -0.17211 0.07030 -2.448 0.01873 *
## Examination -0.25801 0.25388 -1.016 0.31546
## Education -0.87094 0.18303 -4.758 2.43e-05 ***
## Catholic 0.10412 0.03526 2.953 0.00519 **
## Infant.Mortality 1.07705 0.38172 2.822 0.00734 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 7.165 on 41 degrees of freedom
## Multiple R-squared: 0.7067, Adjusted R-squared: 0.671
## F-statistic: 19.76 on 5 and 41 DF, p-value: 5.594e-10
Dari hasil analisis data di atas dapat diketahui beberapa variabel yang signifikan mempengaruhi tingkat fertilitas di Swiss (tingkat alpha 5%), yaitu Agriculture, Catholic, dan Infant Mortality.
Daftar Pustaka
Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) The New S Language. Wadsworth & Brooks/Cole.
Mosteller, F. and Tukey, J. W. (1977) Data Analysis and Regression: A Second Course in Statistics. Addison-Wesley, Reading Mass.