Với project « R thực dụng”, Nhi mong muốn trợ giúp các bạn mới làm quen với R nâng cao kỹ năng lập trình của mình, vì R không chỉ là 1 phần mềm thống kê đơn giản mà còn là một ngôn ngữ lập trình hàm rất thú vị. Bản thân Nhi cũng thấy mình cần thích ứng với xu hướng phát triển hiện đại của ngôn ngữ R (vì R ở thời điểm hiện nay hoàn toàn khác với R mà Nhi biết những năm 2005-2012).
Bài hôm nay Nhi sẽ giới thiệu về hàm « map » trong package purrr. Sự ra đời của nó đã thay đổi hoàn toàn cách thức viết vòng lặp trong R, vốn trước kia dựa trên 2 nhóm công cụ chính là các vòng lặp cổ điển (for, while, …) hoặc những hàm apply (sapply, lapply…).
Công dụng của hàm « map » có thể mô tả ngắn gọn là : « lặp lại nhiều lần một quy trình rồi đóng gói chuỗi kết quả ».
Khi triển khai hàm map, có 2 bộ phận chính tương ứng với 2 câu hỏi : (1) Làm như thế nào ? – hay xác định lộ trình cho vòng lặp và (2) Làm cái gì ? – hay mô tả nội dung 1 hàm f = công việc mà ta muốn làm..
map(.x, ~function(.x,…))
Mỗi bước trên lộ trình của vòng lặp tương ứng với một điều kiện (.x), được dùng như 1 tham số/dữ liệu trong hàm f . Tùy bản chất của dữ liệu đầu vào (thường là 1 list), (.x) có thể là vector, matrix, dataframe, hay 1 con số … , và đóng vai trò dữ liệu đầu vào, tùy chỉnh, tham số để cá biệt hóa nội dung hàm f.
Có nhiều hàm map trong package purrr, mỗi hàm có công dụng khác nhau và xuất kết quả khác nhau. Thí dụ : hàm « map » mặc định xuất kết quả là 1 list, hàm map_df xuất kết quả là 1 dataframe, hàm map_dbl hay map_int xuất kết quả là 1 vector số thực hay số nguyên…
Các bạn còn nhớ dữ liệu điểm thi THPT 2018 ? Chúng ta sẽ dùng nó cho thí dụ minh họa đầu tiên . Nhi tải dữ liệu và trích xuất riêng điểm môn Toán
library(tidyverse)
## -- Attaching packages -------------------------------------------- tidyverse 1.2.1 --
## v ggplot2 3.0.0 v purrr 0.2.5
## v tibble 1.4.2 v dplyr 0.7.6
## v tidyr 0.8.1 v stringr 1.3.1
## v readr 1.1.1 v forcats 0.3.0
## -- Conflicts ----------------------------------------------- tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
df=read.csv("https://raw.githubusercontent.com/kinokoberuji/R-Tutorials/master/Provinces.csv")%>%as_tibble()
df%>%filter(Math!="NA")%>%dplyr::select(Math,Province)->math_df
Object math_df có thể xem như 1 quần thể, với đại lượng cần khảo sát là điểm thi môn Toán. Kích thước khá lớn của quần thể này nhằm chứng tỏ tốc độ tính toán rất nhanh khi sử dụng hàm map
Thí dụ đầu tiên, ta muốn đếm số thí sinh cho mỗi tỉnh thành. Phép đếm này có thể thực hiện dễ dàng bằng hàm group_by và tally của package dplyr:
math_df%>%group_by(Province)%>%tally()
Tuy nhiên ta còn có thể làm bằng hàm map: Do kết quả đếm là 1 số nguyên, nên ta dùng hàm map_int
math_df%>%split(.$Province)%>%
map_int(~nrow(.x))
## AN GIANG BA RIA - VUNG TAU BAC GIANG BAC KAN
## 0 11831 19526 2831
## BAC LIEU BAC NINH BEN TRE BINH DINH
## 5335 14790 11705 17724
## BINH DUONG BINH PHUOC BINH THUAN CA MAU
## 11263 10180 11675 9222
## CAN THO CAO BANG DA NANG DAK LAK
## 13013 4360 6094 21884
## DAK NONG DIEN BIEN DONG NAI DONG THAP
## 6327 5372 28595 14331
## GIA LAI HA HA GIANG HA NAM
## 12719 16211 3089 8657
## HA NOI HAI DUONG HAI PHONG HAU GIANG
## 37993 19933 5094 6176
## HO CHI MINH HOA BINH HUNG YEN KHANH HOA
## 78035 8903 12860 13471
## KIEN GIANG KON TUM LAI CHAU LAM DONG
## 13413 4425 3204 14928
## LANG SON LAO CAO LONG AN NAM DINH
## 8813 6187 14045 5086
## NGHE AN NINH BINH NINH THUAN PHU THO
## 0 9569 5738 13633
## PHU YEN QUANG BINH QUANG NAM QUANG NGAI
## 10690 9546 17463 12598
## QUANG NINH QUANG TRI SOC TRANG SON LA
## 0 7775 9300 0
## TAY NINH THAI BINH THAI NGUYEN THANH HOA
## 8834 21401 8965 14038
## THUA THIEN - HUE TIEN GIANG TRA VINH TUYEN QUANG
## 12303 14088 8152 7542
## VINH LONG VINH PHUC YEN BAI
## 10550 12565 6974
Phép tính này có thể phức tạp hơn, như trong thí dụ sau đây , Nhi dùng 1 hàm map để thi hành hàng loạt quy trình ước tính bách Phân vị thứ 50,75,90,95,97.5 và 99 theo phương pháp Harell Davis cho điểm số mô toán ở mỗi tỉnh thành:
math_df %>% split(.$Province) %>%
map(.,~ Hmisc::hdquantile(.x$Math,probs =c(0.5,0.75,0.9,0.95,0.975,0.99)))->hdquantile
hdquantile
## $`AN GIANG`
## [1] NA NA NA NA NA NA
##
## $`BA RIA - VUNG TAU`
## 0.500 0.750 0.900 0.950 0.975 0.990
## 5.400000 6.199905 6.800000 7.200000 7.563219 7.914723
##
## $`BAC GIANG`
## 0.500 0.750 0.900 0.950 0.975 0.990
## 4.600000 5.792610 6.600047 7.189397 7.590387 7.999703
##
## $`BAC KAN`
## 0.500 0.750 0.900 0.950 0.975 0.990
## 3.802286 4.796259 5.620354 6.176663 6.656565 7.312336
##
## $`BAC LIEU`
## 0.500 0.750 0.900 0.950 0.975 0.990
## 4.999999 5.799994 6.416026 6.839013 7.246998 7.757645
##
## $`BAC NINH`
## 0.500 0.750 0.900 0.950 0.975 0.990
## 5.000000 6.200000 7.008658 7.589541 7.968934 8.289604
##
## $`BEN TRE`
## 0.500 0.750 0.900 0.950 0.975 0.990
## 5.000639 5.803305 6.587759 6.964479 7.204021 7.601284
##
## $`BINH DINH`
## 0.500 0.750 0.900 0.950 0.975 0.990
## 5.000000 5.999996 6.687782 7.184730 7.424387 7.835696
##
## $`BINH DUONG`
## 0.500 0.750 0.900 0.950 0.975 0.990
## 5.200003 6.000000 6.637496 7.005745 7.401851 7.818840
##
## $`BINH PHUOC`
## 0.500 0.750 0.900 0.950 0.975 0.990
## 5.000000 5.999208 6.602674 7.031692 7.400041 7.799399
##
## $`BINH THUAN`
## 0.500 0.750 0.900 0.950 0.975 0.990
## 5.000279 5.800391 6.558797 6.915288 7.202357 7.604272
##
## $`CA MAU`
## 0.500 0.750 0.900 0.950 0.975 0.990
## 4.600074 5.597877 6.204399 6.608821 7.000109 7.389151
##
## $`CAN THO`
## 0.500 0.750 0.900 0.950 0.975 0.990
## 4.948913 5.800121 6.600000 7.001230 7.406297 7.814570
##
## $`CAO BANG`
## 0.500 0.750 0.900 0.950 0.975 0.990
## 3.612234 4.796204 5.786611 6.268932 6.638211 7.109105
##
## $`DA NANG`
## 0.500 0.750 0.900 0.950 0.975 0.990
## 5.200024 6.199945 6.929389 7.380615 7.680195 8.183563
##
## $`DAK LAK`
## 0.500 0.750 0.900 0.950 0.975 0.990
## 4.600000 5.799993 6.600000 7.000304 7.400001 7.800323
##
## $`DAK NONG`
## 0.500 0.750 0.900 0.950 0.975 0.990
## 4.689001 5.651065 6.404651 6.809554 7.213390 7.710722
##
## $`DIEN BIEN`
## 0.500 0.750 0.900 0.950 0.975 0.990
## 3.999999 4.997339 5.846457 6.403061 6.894208 7.574555
##
## $`DONG NAI`
## 0.500 0.750 0.900 0.950 0.975 0.990
## 5.000002 5.821474 6.600000 6.998716 7.215888 7.605066
##
## $`DONG THAP`
## 0.500 0.750 0.900 0.950 0.975 0.990
## 5.005363 5.835720 6.599980 6.999996 7.308687 7.634320
##
## $`GIA LAI`
## 0.500 0.750 0.900 0.950 0.975 0.990
## 4.600007 5.662857 6.528453 6.999836 7.390578 7.795967
##
## $HA
## 0.500 0.750 0.900 0.950 0.975 0.990
## 4.600001 5.793788 6.617909 7.200128 7.601877 8.000393
##
## $`HA GIANG`
## 0.500 0.750 0.900 0.950 0.975 0.990
## 3.199973 4.197598 5.259416 6.229332 7.381538 8.859825
##
## $`HA NAM`
## 0.500 0.750 0.900 0.950 0.975 0.990
## 5.389075 6.222072 7.000000 7.400356 7.798130 8.089240
##
## $`HA NOI`
## 0.500 0.750 0.900 0.950 0.975 0.990
## 5.518258 6.400000 7.200000 7.600000 7.999888 8.232922
##
## $`HAI DUONG`
## 0.500 0.750 0.900 0.950 0.975 0.990
## 5.002566 6.132803 6.879413 7.399975 7.784518 8.132778
##
## $`HAI PHONG`
## 0.500 0.750 0.900 0.950 0.975 0.990
## 5.399776 6.306874 7.019763 7.532636 7.835441 8.348669
##
## $`HAU GIANG`
## 0.500 0.750 0.900 0.950 0.975 0.990
## 4.497970 5.400000 6.199130 6.597712 6.979154 7.352020
##
## $`HO CHI MINH`
## 0.500 0.750 0.900 0.950 0.975 0.990
## 5.402175 6.200000 6.847869 7.259305 7.600000 8.000000
##
## $`HOA BINH`
## 0.500 0.750 0.900 0.950 0.975 0.990
## 3.400000 4.568732 5.942534 6.727072 7.382928 8.085400
##
## $`HUNG YEN`
## 0.500 0.750 0.900 0.950 0.975 0.990
## 4.999759 6.114800 7.000000 7.520065 7.817950 8.208815
##
## $`KHANH HOA`
## 0.500 0.750 0.900 0.950 0.975 0.990
## 5.000000 5.999999 6.608153 7.029322 7.400095 7.799822
##
## $`KIEN GIANG`
## 0.500 0.750 0.900 0.950 0.975 0.990
## 4.600000 5.580261 6.200390 6.601734 7.000125 7.400027
##
## $`KON TUM`
## 0.500 0.750 0.900 0.950 0.975 0.990
## 4.740124 5.829327 6.759417 7.208534 7.582251 8.022827
##
## $`LAI CHAU`
## 0.500 0.750 0.900 0.950 0.975 0.990
## 4.199999 4.996757 5.796905 6.232124 6.623815 7.289272
##
## $`LAM DONG`
## 0.500 0.750 0.900 0.950 0.975 0.990
## 5.200000 6.000000 6.600000 7.000000 7.370035 7.779855
##
## $`LANG SON`
## 0.500 0.750 0.900 0.950 0.975 0.990
## 3.800000 4.799994 5.796326 6.355083 6.801790 7.343663
##
## $`LAO CAO`
## 0.500 0.750 0.900 0.950 0.975 0.990
## 4.424726 5.448715 6.399516 6.805285 7.243621 7.705071
##
## $`LONG AN`
## 0.500 0.750 0.900 0.950 0.975 0.990
## 5.000000 5.800012 6.599788 6.999757 7.333981 7.608846
##
## $`NAM DINH`
## 0.500 0.750 0.900 0.950 0.975 0.990
## 5.629939 6.573560 7.200025 7.598604 7.894011 8.214838
##
## $`NGHE AN`
## [1] NA NA NA NA NA NA
##
## $`NINH BINH`
## 0.500 0.750 0.900 0.950 0.975 0.990
## 5.200239 6.203598 7.084748 7.589983 7.884826 8.280619
##
## $`NINH THUAN`
## 0.500 0.750 0.900 0.950 0.975 0.990
## 4.598125 5.599795 6.397556 6.798608 7.105838 7.539479
##
## $`PHU THO`
## 0.500 0.750 0.900 0.950 0.975 0.990
## 4.600509 5.779899 6.600007 7.098902 7.585875 8.002866
##
## $`PHU YEN`
## 0.500 0.750 0.900 0.950 0.975 0.990
## 4.799998 5.800000 6.599776 7.001897 7.404600 7.858065
##
## $`QUANG BINH`
## 0.500 0.750 0.900 0.950 0.975 0.990
## 4.199971 5.372796 6.220980 6.798398 7.212988 7.762785
##
## $`QUANG NAM`
## 0.500 0.750 0.900 0.950 0.975 0.990
## 4.800000 5.800351 6.603358 7.161329 7.542755 7.965503
##
## $`QUANG NGAI`
## 0.500 0.750 0.900 0.950 0.975 0.990
## 4.999999 6.000000 6.799613 7.200003 7.597685 7.998864
##
## $`QUANG NINH`
## [1] NA NA NA NA NA NA
##
## $`QUANG TRI`
## 0.500 0.750 0.900 0.950 0.975 0.990
## 4.614974 5.800038 6.616468 7.183950 7.556546 7.896133
##
## $`SOC TRANG`
## 0.500 0.750 0.900 0.950 0.975 0.990
## 4.450690 5.400004 6.202225 6.746961 7.109587 7.469744
##
## $`SON LA`
## [1] NA NA NA NA NA NA
##
## $`TAY NINH`
## 0.500 0.750 0.900 0.950 0.975 0.990
## 5.001001 5.803440 6.598148 6.989110 7.238366 7.732689
##
## $`THAI BINH`
## 0.500 0.750 0.900 0.950 0.975 0.990
## 5.224243 6.313677 7.063399 7.594094 7.809082 8.203210
##
## $`THAI NGUYEN`
## 0.500 0.750 0.900 0.950 0.975 0.990
## 4.389841 5.418470 6.399962 6.976738 7.361931 7.802083
##
## $`THANH HOA`
## 0.500 0.750 0.900 0.950 0.975 0.990
## 4.403070 5.802741 6.808442 7.399999 7.799977 8.187719
##
## $`THUA THIEN - HUE`
## 0.500 0.750 0.900 0.950 0.975 0.990
## 4.996273 6.000006 6.800311 7.259246 7.605339 8.011145
##
## $`TIEN GIANG`
## 0.500 0.750 0.900 0.950 0.975 0.990
## 5.318650 6.094053 6.800000 7.199996 7.451647 7.823484
##
## $`TRA VINH`
## 0.500 0.750 0.900 0.950 0.975 0.990
## 4.464179 5.400100 6.199054 6.599700 6.995502 7.389520
##
## $`TUYEN QUANG`
## 0.500 0.750 0.900 0.950 0.975 0.990
## 3.916420 4.823591 5.807896 6.397089 6.864155 7.381700
##
## $`VINH LONG`
## 0.500 0.750 0.900 0.950 0.975 0.990
## 4.998800 5.800000 6.426894 6.843735 7.200953 7.604226
##
## $`VINH PHUC`
## 0.500 0.750 0.900 0.950 0.975 0.990
## 4.999999 6.177329 6.999957 7.402583 7.799999 8.193502
##
## $`YEN BAI`
## 0.500 0.750 0.900 0.950 0.975 0.990
## 3.800545 4.960180 5.997619 6.602036 7.117912 7.639461
Ghi chú: phép ước tính phân vị theo Harell Davis có sử dụng bootstrap, nên rất phổ quát và không phụ thuộc vào đặc tính phân bố, khác với mô tả thông thường. Các bạn có thể tin chắc rằng nếu ở một tỉnh nào đó mà có HDquantile thứ 99 cao hơn trị số này cho quần thể chung, thì ở khu vực đó đã có điều bất thường xảy ra, thí dụ ở Hà Giang trị số HDQ 99 là 8.859, cao hơn rất nhiều so với cả nước (chỉ có 8.0 điểm)
Hmisc::hdquantile(math_df$Math,probs =c(0.5,0.75,0.9,0.95,0.975,0.99))
## 0.500 0.750 0.900 0.950 0.975 0.990
## 5.0 6.0 6.8 7.2 7.6 8.0
Một thí dụ khác, lần này ta làm 1 thí nghiệm: Từ quần thể 741024 thí sinh cả nước, ta sẽ chọn mẫu ngẫu nhiên từ 1000 đến 30000 cá thể và tính giá trị trung bình, trung vị, bách phân vị thứ 5 và thứ 99 cho từng mẫu.
Đây là 1 vòng lặp có nội dung: Chọn mẫu n lần với kích thước tăng dần từ 1000 : 30000, và mỗi lần xuất ra 5 chỉ số thống kê mô tả như trên.
Ta có thể dùng vòng lặp for loop để thực hiện, nhưng ở đây Nhi dùng hàm map.
Vì mỗi hàm map chỉ làm 1 công việc duy nhất, nên quy trình cần kết nối 2 hàm map, hàm map thứ nhất làm việc chọn mẫu, hàm map thứ hai làm thống kê mô tả. Do hàm map thứ hai xuất ra 1 dataframe nên nó sẽ là hàm map_df
math_pop<-math_df$Math
sample_n=seq(from=1000, to=30000,by=1000)
s<-map(sample_n,~sample(math_pop,.x, replace = F))%>%
map_df(~data_frame(Size=length(.x),
Mean=mean(.x),
Median=median(.x),
P5=quantile(.x,probs=0.05),
P99=quantile(.x,probs=0.99),
))
s
Kết quả này có thể đi vào 1 hàm ggplot để vẽ biểu đồ, nếu bạn thích
s%>%ggplot(aes(x=Size))+
geom_pointrange(aes(y=Median,
ymin=P5,ymax=P99,
col=Size),
show.legend = F)+
theme_bw()+
scale_y_continuous(limits = c(0,10))+
geom_hline(yintercept = median(math_pop),linetype=2,col="red")+
coord_flip()
Trong thí dụ tiếp theo, Nhi sẽ dùng bộ dữ liệu gapminder, mục tiêu là dựng 5 mô hình khác nhau ước lượng tuổi thọ trung bình theo dân số, thu nhập bình quân, năm và yếu tố địa lý, cho từng quốc gia
library(gapminder)
gapminder
5 mô hình cần dựng có nội dung:
# a list of formula
f1 = lifeExp ~ pop
f2 = lifeExp ~ gdpPercap
f3 = lifeExp ~ pop + gdpPercap
f4 = lifeExp ~ pop + gdpPercap + year
f5 = lifeExp ~ pop + gdpPercap + year + continent
f1
## lifeExp ~ pop
f2
## lifeExp ~ gdpPercap
f3
## lifeExp ~ pop + gdpPercap
f4
## lifeExp ~ pop + gdpPercap + year
f5
## lifeExp ~ pop + gdpPercap + year + continent
Ta có thể kết hợp 2 hàm map, hàm thứ nhất dựng 5 mô hình với 5 công thức khác nhau, hàm map thứ 2 trích xuất kết quả summary cho từng mô hình
formulas <- list(f1,f2,f3,f4,f5)
mod <- map (formulas, ~ lm(.x, data=gapminder))%>%
map(~ summary(.x))
mod
## [[1]]
##
## Call:
## lm(formula = .x, data = gapminder)
##
## Residuals:
## Min 1Q Median 3Q Max
## -35.70 -11.13 1.07 11.45 22.91
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 5.924e+01 3.243e-01 182.688 < 2e-16 ***
## pop 7.904e-09 2.943e-09 2.685 0.00731 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 12.89 on 1702 degrees of freedom
## Multiple R-squared: 0.004219, Adjusted R-squared: 0.003634
## F-statistic: 7.212 on 1 and 1702 DF, p-value: 0.007314
##
##
## [[2]]
##
## Call:
## lm(formula = .x, data = gapminder)
##
## Residuals:
## Min 1Q Median 3Q Max
## -82.754 -7.758 2.176 8.225 18.426
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 5.396e+01 3.150e-01 171.29 <2e-16 ***
## gdpPercap 7.649e-04 2.579e-05 29.66 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 10.49 on 1702 degrees of freedom
## Multiple R-squared: 0.3407, Adjusted R-squared: 0.3403
## F-statistic: 879.6 on 1 and 1702 DF, p-value: < 2.2e-16
##
##
## [[3]]
##
## Call:
## lm(formula = .x, data = gapminder)
##
## Residuals:
## Min 1Q Median 3Q Max
## -82.754 -7.745 2.055 8.212 18.534
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 5.365e+01 3.225e-01 166.36 < 2e-16 ***
## pop 9.728e-09 2.385e-09 4.08 4.72e-05 ***
## gdpPercap 7.676e-04 2.568e-05 29.89 < 2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 10.44 on 1701 degrees of freedom
## Multiple R-squared: 0.3471, Adjusted R-squared: 0.3463
## F-statistic: 452.2 on 2 and 1701 DF, p-value: < 2.2e-16
##
##
## [[4]]
##
## Call:
## lm(formula = .x, data = gapminder)
##
## Residuals:
## Min 1Q Median 3Q Max
## -67.497 -7.075 1.121 7.701 19.640
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -4.115e+02 2.767e+01 -14.872 < 2e-16 ***
## pop 6.353e-09 2.218e-09 2.864 0.00423 **
## gdpPercap 6.729e-04 2.444e-05 27.529 < 2e-16 ***
## year 2.354e-01 1.400e-02 16.812 < 2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 9.673 on 1700 degrees of freedom
## Multiple R-squared: 0.4402, Adjusted R-squared: 0.4392
## F-statistic: 445.6 on 3 and 1700 DF, p-value: < 2.2e-16
##
##
## [[5]]
##
## Call:
## lm(formula = .x, data = gapminder)
##
## Residuals:
## Min 1Q Median 3Q Max
## -28.4051 -4.0550 0.2317 4.5073 20.0217
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -5.185e+02 1.989e+01 -26.062 <2e-16 ***
## pop 1.791e-09 1.634e-09 1.096 0.273
## gdpPercap 2.985e-04 2.002e-05 14.908 <2e-16 ***
## year 2.863e-01 1.006e-02 28.469 <2e-16 ***
## continentAmericas 1.429e+01 4.946e-01 28.898 <2e-16 ***
## continentAsia 9.375e+00 4.719e-01 19.869 <2e-16 ***
## continentEurope 1.936e+01 5.182e-01 37.361 <2e-16 ***
## continentOceania 2.056e+01 1.469e+00 13.995 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 6.883 on 1696 degrees of freedom
## Multiple R-squared: 0.7172, Adjusted R-squared: 0.716
## F-statistic: 614.5 on 7 and 1696 DF, p-value: < 2.2e-16
Bây giờ ta thay đổi hàm map thứ hai thành map_df để trích xuất riêng trị số AIC và công thức mỗi mô hình
mod <- map (formulas, ~ lm(.x, data=gapminder))%>%
map_df(~ data_frame(formula = format(formula(.x)),
AIC=AIC(.x),
stringAsFactors=F))
mod
Một thủ thuật khác, đó là nêu trực tiếp tên của thành phần cần trích xuất từ dữ liệu đầu ra của hàm map đi trước (trở thành .x của hàm map đi sau).
gapminder %>% split(.$country) %>%
map (.,~ lm(lifeExp ~ year, data=.x))%>%
map(~ summary(.x))%>%
map_dbl("r.squared")
## Afghanistan Albania Algeria
## 0.94771226 0.91057777 0.98511721
## Angola Argentina Australia
## 0.88781463 0.99556810 0.97964774
## Austria Bahrain Bangladesh
## 0.99213401 0.96673981 0.98936087
## Belgium Benin Bolivia
## 0.99454056 0.96660199 0.98454156
## Bosnia and Herzegovina Botswana Brazil
## 0.89569829 0.03402340 0.99804741
## Bulgaria Burkina Faso Burundi
## 0.54654217 0.91871050 0.76599597
## Cambodia Cameroon Canada
## 0.63869222 0.68017839 0.99638552
## Central African Republic Chad Chile
## 0.49324448 0.87237550 0.98279710
## China Colombia Comoros
## 0.87127734 0.96787344 0.99685076
## Congo, Dem. Rep. Congo, Rep. Costa Rica
## 0.34820278 0.51966079 0.96174767
## Cote d'Ivoire Croatia Cuba
## 0.28337240 0.93243047 0.92406716
## Czech Republic Denmark Djibouti
## 0.91668191 0.97066797 0.97437134
## Dominican Republic Ecuador Egypt
## 0.97060781 0.99456626 0.99030424
## El Salvador Equatorial Guinea Eritrea
## 0.95567201 0.99686864 0.95727334
## Ethiopia Finland France
## 0.96850263 0.99383835 0.99762458
## Gabon Gambia Germany
## 0.81276621 0.98923562 0.98950568
## Ghana Greece Guatemala
## 0.98409873 0.97196085 0.99666377
## Guinea Guinea-Bissau Haiti
## 0.97831518 0.98455829 0.98761338
## Honduras Hong Kong, China Hungary
## 0.97730026 0.97230183 0.79501875
## Iceland India Indonesia
## 0.97032657 0.96843652 0.99711418
## Iran Iraq Ireland
## 0.99501535 0.54578420 0.98414574
## Israel Italy Jamaica
## 0.99478290 0.99336612 0.80565904
## Japan Jordan Kenya
## 0.95959563 0.96975008 0.44255729
## Korea, Dem. Rep. Korea, Rep. Kuwait
## 0.70306306 0.98765101 0.95235423
## Lebanon Lesotho Liberia
## 0.94172582 0.08485635 0.51175640
## Libya Madagascar Malawi
## 0.98333149 0.99465364 0.83995446
## Malaysia Mali Mauritania
## 0.94650639 0.99545140 0.99767430
## Mauritius Mexico Mongolia
## 0.93478457 0.98520444 0.98731309
## Montenegro Morocco Mozambique
## 0.80186521 0.99458312 0.77427932
## Myanmar Namibia Nepal
## 0.87937750 0.43702163 0.99154171
## Netherlands New Zealand Nicaragua
## 0.98221566 0.95358464 0.99677615
## Niger Nigeria Norway
## 0.89768664 0.87010508 0.96290057
## Oman Pakistan Panama
## 0.97479461 0.99724965 0.95119516
## Paraguay Peru Philippines
## 0.98298650 0.98847401 0.99142260
## Poland Portugal Puerto Rico
## 0.83966315 0.96903508 0.90781912
## Reunion Romania Rwanda
## 0.96607180 0.80556666 0.01715964
## Sao Tome and Principe Saudi Arabia Senegal
## 0.95525936 0.97208439 0.99054417
## Serbia Sierra Leone Singapore
## 0.87880538 0.96015054 0.98794751
## Slovak Republic Slovenia Somalia
## 0.79174822 0.96604327 0.84442863
## South Africa Spain Sri Lanka
## 0.31246865 0.96489456 0.94771469
## Sudan Swaziland Sweden
## 0.99214243 0.06821087 0.99548216
## Switzerland Syria Taiwan
## 0.99739086 0.98416512 0.95707113
## Tanzania Thailand Togo
## 0.76421876 0.96738440 0.90580373
## Trinidad and Tobago Tunisia Turkey
## 0.79800744 0.98070422 0.98533264
## Uganda United Kingdom United States
## 0.34215382 0.98443596 0.98592016
## Uruguay Venezuela Vietnam
## 0.97683072 0.94652607 0.98941189
## West Bank and Gaza Yemen, Rep. Zambia
## 0.97048087 0.98117240 0.05983644
## Zimbabwe
## 0.05623196
Trong thí dụ minh họa tiếp theo, Nhi sẽ dùng dữ liệu cigarette , với nội dung diễn tiến giá bán lẻ thuốc lá tại 49 bang của Mỹ từ năm 1962 đến 1992.
library(gamlss)
## Loading required package: splines
## Loading required package: gamlss.data
## Loading required package: gamlss.dist
## Loading required package: MASS
##
## Attaching package: 'MASS'
## The following object is masked from 'package:dplyr':
##
## select
## Loading required package: nlme
##
## Attaching package: 'nlme'
## The following object is masked from 'package:dplyr':
##
## collapse
## Loading required package: parallel
## ********** GAMLSS Version 5.1-0 **********
## For more on GAMLSS look at http://www.gamlss.org/
## Type gamlssNews() to see new features/changes/bug fixes.
nC<-detectCores()
cigarette=read.csv("https://raw.github.com/vincentarelbundock/Rdatasets/master/csv/plm/Cigar.csv")
cigarette$state=factor(cigarette$state)
cigarette$year=factor(cigarette$year-62)
cigarette
Như ta biết, package gamlss có hàm fitDist cho phép thăm dò và xác định tự động family (họ phân phối) phù hợp nhất cho một vector biến kết quả. Bây giờ Nhi sẽ thực hiện quy trình này cho dữ liệu của từng bang (state) trong số 49 bang.
Nhi sử dụng 3 hàm map liên tiếp, hàm map thứ nhất thi hành hàm fitDist cho dữ liệu của mỗi bang trong danh sách 49 bang, theo thứ tự từ 1 đến 49, bộ phận .x do đó được đưa vào data của hàm fitDist, kết quả xuất ra là 1 list - tiếp tục đi vào hàm map thứ hai, có mục tiêu thi hành hàm summary, kết quả xuất ra cũng là 1 list và đi tiếp vào hàm map thứ 3 có bản chất là măpdf để xuất ra kết quả là 1 dataframe, bao gồm: độ tự do df, trung vị Mu, AIC, BIC của mô hình tối ưu, và tên phân phối tối ưu được chọn…
## *******************************************************************
## Family: c("WEI3", "Weibull type 3")
##
## Call: gamlssML(formula = y, family = DIST[i], parallel = "multicore",
## ncpus = ..2, data = sys.parent())
##
## Fitting method: "nlminb"
##
##
## Coefficient(s):
## Estimate Std. Error t value Pr(>|t|)
## eta.mu 4.678352 0.018042 259.3036 < 2.22e-16 ***
## eta.sigma 2.512718 0.147394 17.0476 < 2.22e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Degrees of Freedom for the fit: 2 Residual Deg. of Freedom 28
## Global Deviance: 224.807
## AIC: 228.807
## SBC: 231.61
## *******************************************************************
## Family: c("WEI3", "Weibull type 3")
##
## Call: gamlssML(formula = y, family = DIST[i], parallel = "multicore",
## ncpus = ..2, data = sys.parent())
##
## Fitting method: "nlminb"
##
##
## Coefficient(s):
## Estimate Std. Error t value Pr(>|t|)
## eta.mu 4.7320342 0.0188179 251.4646 < 2.22e-16 ***
## eta.sigma 2.4586170 0.1496169 16.4327 < 2.22e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Degrees of Freedom for the fit: 2 Residual Deg. of Freedom 28
## Global Deviance: 233.302
## AIC: 237.302
## SBC: 240.104
## *******************************************************************
## Family: c("WEI", "Weibull")
##
## Call: gamlssML(formula = y, family = DIST[i], parallel = "multicore",
## ncpus = ..2, data = sys.parent())
##
## Fitting method: "nlminb"
##
##
## Coefficient(s):
## Estimate Std. Error t value Pr(>|t|)
## eta.mu 4.7888370 0.0149126 321.1268 < 2.22e-16 ***
## eta.sigma 2.5587557 0.1484018 17.2421 < 2.22e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Degrees of Freedom for the fit: 2 Residual Deg. of Freedom 28
## Global Deviance: 226.204
## AIC: 230.204
## SBC: 233.006
## *******************************************************************
## Family: c("GG", "generalised Gamma Lopatatsidis-Green")
##
## Call: gamlssML(formula = y, family = DIST[i], parallel = "multicore",
## ncpus = ..2, data = sys.parent())
##
## Fitting method: "nlminb"
##
##
## Coefficient(s):
## Estimate Std. Error t value Pr(>|t|)
## eta.mu 4.8745447 0.0431458 112.97834 < 2.22e-16 ***
## eta.sigma -2.4628458 0.3477841 -7.08154 1.4257e-12 ***
## eta.nu 27.5213998 21.8974387 1.25683 0.20881
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Degrees of Freedom for the fit: 3 Residual Deg. of Freedom 27
## Global Deviance: 256.405
## AIC: 262.405
## SBC: 266.609
## *******************************************************************
## Family: c("IG", "Inverse Gaussian")
##
## Call: gamlssML(formula = y, family = DIST[i], parallel = "multicore",
## ncpus = ..2, data = sys.parent())
##
## Fitting method: "nlminb"
##
##
## Coefficient(s):
## Estimate Std. Error t value Pr(>|t|)
## eta.mu 4.7712491 0.0277281 172.0727 < 2.22e-16 ***
## eta.sigma -4.2703330 0.1290993 -33.0779 < 2.22e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Degrees of Freedom for the fit: 2 Residual Deg. of Freedom 28
## Global Deviance: 257.302
## AIC: 261.302
## SBC: 264.105
## *******************************************************************
## Family: c("WEI3", "Weibull type 3")
##
## Call: gamlssML(formula = y, family = DIST[i], parallel = "multicore",
## ncpus = ..2, data = sys.parent())
##
## Fitting method: "nlminb"
##
##
## Coefficient(s):
## Estimate Std. Error t value Pr(>|t|)
## eta.mu 5.014350 0.016828 297.9769 < 2.22e-16 ***
## eta.sigma 2.577347 0.140682 18.3204 < 2.22e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Degrees of Freedom for the fit: 2 Residual Deg. of Freedom 28
## Global Deviance: 240.032
## AIC: 244.032
## SBC: 246.834
## *******************************************************************
## Family: c("IG", "Inverse Gaussian")
##
## Call: gamlssML(formula = y, family = DIST[i], parallel = "multicore",
## ncpus = ..2, data = sys.parent())
##
## Fitting method: "nlminb"
##
##
## Coefficient(s):
## Estimate Std. Error t value Pr(>|t|)
## eta.mu 5.1012070 0.0677332 75.3132 < 2.22e-16 ***
## eta.sigma -3.5421818 0.1290994 -27.4376 < 2.22e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Degrees of Freedom for the fit: 2 Residual Deg. of Freedom 28
## Global Deviance: 325.936
## AIC: 329.936
## SBC: 332.738
## *******************************************************************
## Family: c("WEI3", "Weibull type 3")
##
## Call: gamlssML(formula = y, family = DIST[i], parallel = "multicore",
## ncpus = ..2, data = sys.parent())
##
## Fitting method: "nlminb"
##
##
## Coefficient(s):
## Estimate Std. Error t value Pr(>|t|)
## eta.mu 4.8392658 0.0119944 403.4620 < 2.22e-16 ***
## eta.sigma 2.9228997 0.1518323 19.2508 < 2.22e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Degrees of Freedom for the fit: 2 Residual Deg. of Freedom 28
## Global Deviance: 213.514
## AIC: 217.514
## SBC: 220.316
## *******************************************************************
## Family: c("WEI", "Weibull")
##
## Call: gamlssML(formula = y, family = DIST[i], parallel = "multicore",
## ncpus = ..2, data = sys.parent())
##
## Fitting method: "nlminb"
##
##
## Coefficient(s):
## Estimate Std. Error t value Pr(>|t|)
## eta.mu 4.8192445 0.0132239 364.4331 < 2.22e-16 ***
## eta.sigma 2.6786791 0.1487585 18.0069 < 2.22e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Degrees of Freedom for the fit: 2 Residual Deg. of Freedom 28
## Global Deviance: 221.253
## AIC: 225.253
## SBC: 228.055
## *******************************************************************
## Family: c("GA", "Gamma")
##
## Call: gamlssML(formula = y, family = DIST[i], parallel = "multicore",
## ncpus = ..2, data = sys.parent())
##
## Fitting method: "nlminb"
##
##
## Coefficient(s):
## Estimate Std. Error t value Pr(>|t|)
## eta.mu 4.6522128 0.0246736 188.5500 < 2.22e-16 ***
## eta.sigma -2.0014216 0.1287082 -15.5501 < 2.22e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Degrees of Freedom for the fit: 2 Residual Deg. of Freedom 28
## Global Deviance: 243.817
## AIC: 247.817
## SBC: 250.619
## *******************************************************************
## Family: c("WEI", "Weibull")
##
## Call: gamlssML(formula = y, family = DIST[i], parallel = "multicore",
## ncpus = ..2, data = sys.parent())
##
## Fitting method: "nlminb"
##
##
## Coefficient(s):
## Estimate Std. Error t value Pr(>|t|)
## eta.mu 4.8851209 0.0166953 292.6039 < 2.22e-16 ***
## eta.sigma 2.4384137 0.1498708 16.2701 < 2.22e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Degrees of Freedom for the fit: 2 Residual Deg. of Freedom 28
## Global Deviance: 240.763
## AIC: 244.763
## SBC: 247.565
## *******************************************************************
## Family: c("exGAUS", "ex-Gaussian")
##
## Call: gamlssML(formula = y, family = DIST[i], parallel = "multicore",
## ncpus = ..2, data = sys.parent())
##
## Fitting method: "nlminb"
##
##
## Coefficient(s):
## Estimate Std. Error t value Pr(>|t|)
## eta.mu 129.803083 1.413792 91.8120 < 2.22e-16 ***
## eta.sigma 1.011647 0.373343 2.7097 0.0067345 **
## eta.nu 2.511244 0.211756 11.8591 < 2.22e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Degrees of Freedom for the fit: 3 Residual Deg. of Freedom 27
## Global Deviance: 222.245
## AIC: 228.245
## SBC: 232.448
## *******************************************************************
## Family: c("GA", "Gamma")
##
## Call: gamlssML(formula = y, family = DIST[i], parallel = "multicore",
## ncpus = ..2, data = sys.parent())
##
## Fitting method: "nlminb"
##
##
## Coefficient(s):
## Estimate Std. Error t value Pr(>|t|)
## eta.mu 4.7160256 0.0162293 290.5870 < 2.22e-16 ***
## eta.sigma -2.4203378 0.1289297 -18.7725 < 2.22e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Degrees of Freedom for the fit: 2 Residual Deg. of Freedom 28
## Global Deviance: 222.719
## AIC: 226.719
## SBC: 229.522
## *******************************************************************
## Family: c("WEI3", "Weibull type 3")
##
## Call: gamlssML(formula = y, family = DIST[i], parallel = "multicore",
## ncpus = ..2, data = sys.parent())
##
## Fitting method: "nlminb"
##
##
## Coefficient(s):
## Estimate Std. Error t value Pr(>|t|)
## eta.mu 4.7247147 0.0210104 224.8750 < 2.22e-16 ***
## eta.sigma 2.3540751 0.1451257 16.2209 < 2.22e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Degrees of Freedom for the fit: 2 Residual Deg. of Freedom 28
## Global Deviance: 236.075
## AIC: 240.075
## SBC: 242.877
## *******************************************************************
## Family: c("WEI3", "Weibull type 3")
##
## Call: gamlssML(formula = y, family = DIST[i], parallel = "multicore",
## ncpus = ..2, data = sys.parent())
##
## Fitting method: "nlminb"
##
##
## Coefficient(s):
## Estimate Std. Error t value Pr(>|t|)
## eta.mu 5.1941539 0.0330977 156.9339 < 2.22e-16 ***
## eta.sigma 1.8684452 0.1454744 12.8438 < 2.22e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Degrees of Freedom for the fit: 2 Residual Deg. of Freedom 28
## Global Deviance: 293
## AIC: 297
## SBC: 299.802
## *******************************************************************
## Family: c("IG", "Inverse Gaussian")
##
## Call: gamlssML(formula = y, family = DIST[i], parallel = "multicore",
## ncpus = ..2, data = sys.parent())
##
## Fitting method: "nlminb"
##
##
## Coefficient(s):
## Estimate Std. Error t value Pr(>|t|)
## eta.mu 4.8204697 0.0184704 260.9835 < 2.22e-16 ***
## eta.sigma -4.7012206 0.1290994 -36.4155 < 2.22e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Degrees of Freedom for the fit: 2 Residual Deg. of Freedom 28
## Global Deviance: 236.447
## AIC: 240.447
## SBC: 243.249
## *******************************************************************
## Family: c("WEI3", "Weibull type 3")
##
## Call: gamlssML(formula = y, family = DIST[i], parallel = "multicore",
## ncpus = ..2, data = sys.parent())
##
## Fitting method: "nlminb"
##
##
## Coefficient(s):
## Estimate Std. Error t value Pr(>|t|)
## eta.mu 4.8962446 0.0108815 449.9615 < 2.22e-16 ***
## eta.sigma 3.0323124 0.1505075 20.1472 < 2.22e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Degrees of Freedom for the fit: 2 Residual Deg. of Freedom 28
## Global Deviance: 208.719
## AIC: 212.719
## SBC: 215.521
## *******************************************************************
## Family: c("WEI", "Weibull")
##
## Call: gamlssML(formula = y, family = DIST[i], parallel = "multicore",
## ncpus = ..2, data = sys.parent())
##
## Fitting method: "nlminb"
##
##
## Coefficient(s):
## Estimate Std. Error t value Pr(>|t|)
## eta.mu 4.8652046 0.0156605 310.6672 < 2.22e-16 ***
## eta.sigma 2.5076148 0.1411398 17.7669 < 2.22e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Degrees of Freedom for the fit: 2 Residual Deg. of Freedom 28
## Global Deviance: 233.06
## AIC: 237.06
## SBC: 239.862
## *******************************************************************
## Family: c("WEI", "Weibull")
##
## Call: gamlssML(formula = y, family = DIST[i], parallel = "multicore",
## ncpus = ..2, data = sys.parent())
##
## Fitting method: "nlminb"
##
##
## Coefficient(s):
## Estimate Std. Error t value Pr(>|t|)
## eta.mu 4.8270212 0.0158553 304.4428 < 2.22e-16 ***
## eta.sigma 2.4966853 0.1381146 18.0769 < 2.22e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Degrees of Freedom for the fit: 2 Residual Deg. of Freedom 28
## Global Deviance: 230.531
## AIC: 234.531
## SBC: 237.334
## *******************************************************************
## Family: c("WEI", "Weibull")
##
## Call: gamlssML(formula = y, family = DIST[i], parallel = "multicore",
## ncpus = ..2, data = sys.parent())
##
## Fitting method: "nlminb"
##
##
## Coefficient(s):
## Estimate Std. Error t value Pr(>|t|)
## eta.mu 4.8962239 0.0102083 479.6302 < 2.22e-16 ***
## eta.sigma 2.9329448 0.1491804 19.6604 < 2.22e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Degrees of Freedom for the fit: 2 Residual Deg. of Freedom 28
## Global Deviance: 212.292
## AIC: 216.292
## SBC: 219.095
## *******************************************************************
## Family: c("GG", "generalised Gamma Lopatatsidis-Green")
##
## Call: gamlssML(formula = y, family = DIST[i], parallel = "multicore",
## ncpus = ..2, data = sys.parent())
##
## Fitting method: "nlminb"
##
##
## Coefficient(s):
## Estimate Std. Error t value Pr(>|t|)
## eta.mu 4.7644634 0.0225723 211.07595 < 2.22e-16 ***
## eta.sigma -3.3918055 0.4984380 -6.80487 1.0114e-11 ***
## eta.nu 91.7033919 99.4415651 0.92218 0.35643
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Degrees of Freedom for the fit: 3 Residual Deg. of Freedom 27
## Global Deviance: 210.745
## AIC: 216.745
## SBC: 220.948
## *******************************************************************
## Family: c("WEI3", "Weibull type 3")
##
## Call: gamlssML(formula = y, family = DIST[i], parallel = "multicore",
## ncpus = ..2, data = sys.parent())
##
## Fitting method: "nlminb"
##
##
## Coefficient(s):
## Estimate Std. Error t value Pr(>|t|)
## eta.mu 4.686041 0.022751 205.9704 < 2.22e-16 ***
## eta.sigma 2.268052 0.150879 15.0322 < 2.22e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Degrees of Freedom for the fit: 2 Residual Deg. of Freedom 28
## Global Deviance: 240.849
## AIC: 244.849
## SBC: 247.652
## *******************************************************************
## Family: c("WEI", "Weibull")
##
## Call: gamlssML(formula = y, family = DIST[i], parallel = "multicore",
## ncpus = ..2, data = sys.parent())
##
## Fitting method: "nlminb"
##
##
## Coefficient(s):
## Estimate Std. Error t value Pr(>|t|)
## eta.mu 4.90985083 0.00815776 601.8627 < 2.22e-16 ***
## eta.sigma 3.16131037 0.14712482 21.4873 < 2.22e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Degrees of Freedom for the fit: 2 Residual Deg. of Freedom 28
## Global Deviance: 198.492
## AIC: 202.492
## SBC: 205.294
## *******************************************************************
## Family: c("GG", "generalised Gamma Lopatatsidis-Green")
##
## Call: gamlssML(formula = y, family = DIST[i], parallel = "multicore",
## ncpus = ..2, data = sys.parent())
##
## Fitting method: "nlminb"
##
##
## Coefficient(s):
## Estimate Std. Error t value Pr(>|t|)
## eta.mu 4.8162529 0.0308838 155.94734 < 2.22e-16 ***
## eta.sigma -3.2305008 0.6007949 -5.37704 7.5719e-08 ***
## eta.nu 86.7373855 111.8904213 0.77520 0.43822
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Degrees of Freedom for the fit: 3 Residual Deg. of Freedom 27
## Global Deviance: 227.122
## AIC: 233.122
## SBC: 237.325
## *******************************************************************
## Family: c("GG", "generalised Gamma Lopatatsidis-Green")
##
## Call: gamlssML(formula = y, family = DIST[i], parallel = "multicore",
## ncpus = ..2, data = sys.parent())
##
## Fitting method: "nlminb"
##
##
## Coefficient(s):
## Estimate Std. Error t value Pr(>|t|)
## eta.mu 4.77364e+00 4.24935e-04 11233.795 < 2.22e-16 ***
## eta.sigma -5.08788e+00 9.20134e-02 -55.295 < 2.22e-16 ***
## eta.nu 2.65734e+03 1.99990e+00 1328.738 < 2.22e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Degrees of Freedom for the fit: 3 Residual Deg. of Freedom 27
## Global Deviance: 202.552
## AIC: 208.552
## SBC: 212.756
## *******************************************************************
## Family: c("GG", "generalised Gamma Lopatatsidis-Green")
##
## Call: gamlssML(formula = y, family = DIST[i], parallel = "multicore",
## ncpus = ..2, data = sys.parent())
##
## Fitting method: "nlminb"
##
##
## Coefficient(s):
## Estimate Std. Error t value Pr(>|t|)
## eta.mu 5.300845 0.032750 161.85802 < 2.22e-16 ***
## eta.sigma -2.734997 0.368325 -7.42549 1.1235e-13 ***
## eta.nu 47.177242 37.264839 1.26600 0.20551
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Degrees of Freedom for the fit: 3 Residual Deg. of Freedom 27
## Global Deviance: 277.377
## AIC: 283.377
## SBC: 287.58
## *******************************************************************
## Family: c("WEI3", "Weibull type 3")
##
## Call: gamlssML(formula = y, family = DIST[i], parallel = "multicore",
## ncpus = ..2, data = sys.parent())
##
## Fitting method: "nlminb"
##
##
## Coefficient(s):
## Estimate Std. Error t value Pr(>|t|)
## eta.mu 5.469859 0.032100 170.4008 < 2.22e-16 ***
## eta.sigma 1.896078 0.152184 12.4591 < 2.22e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Degrees of Freedom for the fit: 2 Residual Deg. of Freedom 28
## Global Deviance: 310.375
## AIC: 314.375
## SBC: 317.177
## *******************************************************************
## Family: c("WEI3", "Weibull type 3")
##
## Call: gamlssML(formula = y, family = DIST[i], parallel = "multicore",
## ncpus = ..2, data = sys.parent())
##
## Fitting method: "nlminb"
##
##
## Coefficient(s):
## Estimate Std. Error t value Pr(>|t|)
## eta.mu 4.7922560 0.0196999 243.2631 < 2.22e-16 ***
## eta.sigma 2.4083370 0.1407532 17.1104 < 2.22e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Degrees of Freedom for the fit: 2 Residual Deg. of Freedom 28
## Global Deviance: 237.4
## AIC: 241.4
## SBC: 244.202
## *******************************************************************
## Family: c("GB2", "Generalized beta 2 (i.e. of the second kind)")
##
## Call: gamlssML(formula = y, family = DIST[i], parallel = "multicore",
## ncpus = ..2, data = sys.parent())
##
## Fitting method: "nlminb"
##
##
## Coefficient(s):
## Estimate Std. Error t value Pr(>|t|)
## eta.mu 4.7947628 0.0906771 52.87733 < 2.22e-16 ***
## eta.sigma 4.7345252 0.5774689 8.19875 2.2204e-16 ***
## eta.nu -2.6185392 0.5787524 -4.52445 6.0551e-06 ***
## eta.tau 17.3487596 0.6428190 26.98856 < 2.22e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Degrees of Freedom for the fit: 4 Residual Deg. of Freedom 26
## Global Deviance: 209.129
## AIC: 217.129
## SBC: 222.733
## *******************************************************************
## Family: c("BCPE", "Box-Cox Power Exponential")
##
## Call: gamlssML(formula = y, family = DIST[i], parallel = "multicore",
## ncpus = ..2, data = sys.parent())
##
## Fitting method: "nlminb"
##
##
## Coefficient(s):
## Estimate Std. Error t value Pr(>|t|)
## eta.mu 123.3000000 0.0115292 10694.60014 < 2.22e-16 ***
## eta.sigma -2.1019733 0.3909176 -5.37702 7.5727e-08 ***
## eta.nu 3.5091132 2.9358629 1.19526 0.23199
## eta.tau -0.5380959 0.3830909 -1.40462 0.16014
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Degrees of Freedom for the fit: 4 Residual Deg. of Freedom 26
## Global Deviance: 231.916
## AIC: 239.916
## SBC: 245.521
## *******************************************************************
## Family: c("WEI", "Weibull")
##
## Call: gamlssML(formula = y, family = DIST[i], parallel = "multicore",
## ncpus = ..2, data = sys.parent())
##
## Fitting method: "nlminb"
##
##
## Coefficient(s):
## Estimate Std. Error t value Pr(>|t|)
## eta.mu 4.7068201 0.0234847 200.4208 < 2.22e-16 ***
## eta.sigma 2.1056607 0.1440617 14.6164 < 2.22e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Degrees of Freedom for the fit: 2 Residual Deg. of Freedom 28
## Global Deviance: 246.023
## AIC: 250.023
## SBC: 252.826
## *******************************************************************
## Family: c("WEI3", "Weibull type 3")
##
## Call: gamlssML(formula = y, family = DIST[i], parallel = "multicore",
## ncpus = ..2, data = sys.parent())
##
## Fitting method: "nlminb"
##
##
## Coefficient(s):
## Estimate Std. Error t value Pr(>|t|)
## eta.mu 4.83940561 0.00934888 517.6452 < 2.22e-16 ***
## eta.sigma 3.18945706 0.14378252 22.1825 < 2.22e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Degrees of Freedom for the fit: 2 Residual Deg. of Freedom 28
## Global Deviance: 193.358
## AIC: 197.358
## SBC: 200.161
## *******************************************************************
## Family: c("WEI3", "Weibull type 3")
##
## Call: gamlssML(formula = y, family = DIST[i], parallel = "multicore",
## ncpus = ..2, data = sys.parent())
##
## Fitting method: "nlminb"
##
##
## Coefficient(s):
## Estimate Std. Error t value Pr(>|t|)
## eta.mu 4.7923612 0.0262064 182.8700 < 2.22e-16 ***
## eta.sigma 2.1171570 0.1453969 14.5612 < 2.22e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Degrees of Freedom for the fit: 2 Residual Deg. of Freedom 28
## Global Deviance: 254.579
## AIC: 258.579
## SBC: 261.381
## *******************************************************************
## Family: c("WEI", "Weibull")
##
## Call: gamlssML(formula = y, family = DIST[i], parallel = "multicore",
## ncpus = ..2, data = sys.parent())
##
## Fitting method: "nlminb"
##
##
## Coefficient(s):
## Estimate Std. Error t value Pr(>|t|)
## eta.mu 4.7772823 0.0114656 416.6616 < 2.22e-16 ***
## eta.sigma 2.8199982 0.1467386 19.2178 < 2.22e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Degrees of Freedom for the fit: 2 Residual Deg. of Freedom 28
## Global Deviance: 210.571
## AIC: 214.571
## SBC: 217.373
## *******************************************************************
## Family: c("GG", "generalised Gamma Lopatatsidis-Green")
##
## Call: gamlssML(formula = y, family = DIST[i], parallel = "multicore",
## ncpus = ..2, data = sys.parent())
##
## Fitting method: "nlminb"
##
##
## Coefficient(s):
## Estimate Std. Error t value Pr(>|t|)
## eta.mu 4.9866996 0.0202402 246.37641 < 2e-16 ***
## eta.sigma -2.9865294 0.2696834 -11.07420 < 2e-16 ***
## eta.nu 45.5499272 27.4557659 1.65903 0.09711 .
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Degrees of Freedom for the fit: 3 Residual Deg. of Freedom 27
## Global Deviance: 234.644
## AIC: 240.644
## SBC: 244.847
## *******************************************************************
## Family: c("GG", "generalised Gamma Lopatatsidis-Green")
##
## Call: gamlssML(formula = y, family = DIST[i], parallel = "multicore",
## ncpus = ..2, data = sys.parent())
##
## Fitting method: "nlminb"
##
##
## Coefficient(s):
## Estimate Std. Error t value Pr(>|t|)
## eta.mu 4.9195697 0.0272504 180.53186 < 2.22e-16 ***
## eta.sigma -3.3154484 0.6329963 -5.23771 1.6259e-07 ***
## eta.nu 126.1253087 165.5200846 0.76199 0.44606
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Degrees of Freedom for the fit: 3 Residual Deg. of Freedom 27
## Global Deviance: 241.537
## AIC: 247.537
## SBC: 251.741
## *******************************************************************
## Family: c("GA", "Gamma")
##
## Call: gamlssML(formula = y, family = DIST[i], parallel = "multicore",
## ncpus = ..2, data = sys.parent())
##
## Fitting method: "nlminb"
##
##
## Coefficient(s):
## Estimate Std. Error t value Pr(>|t|)
## eta.mu 4.6374115 0.0160103 289.6520 < 2.22e-16 ***
## eta.sigma -2.4339249 0.1289342 -18.8773 < 2.22e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Degrees of Freedom for the fit: 2 Residual Deg. of Freedom 28
## Global Deviance: 217.191
## AIC: 221.191
## SBC: 223.994
## *******************************************************************
## Family: c("WEI", "Weibull")
##
## Call: gamlssML(formula = y, family = DIST[i], parallel = "multicore",
## ncpus = ..2, data = sys.parent())
##
## Fitting method: "nlminb"
##
##
## Coefficient(s):
## Estimate Std. Error t value Pr(>|t|)
## eta.mu 4.813292 0.012137 396.5809 < 2.22e-16 ***
## eta.sigma 2.763167 0.149888 18.4349 < 2.22e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Degrees of Freedom for the fit: 2 Residual Deg. of Freedom 28
## Global Deviance: 216.49
## AIC: 220.49
## SBC: 223.292
## *******************************************************************
## Family: c("WEI", "Weibull")
##
## Call: gamlssML(formula = y, family = DIST[i], parallel = "multicore",
## ncpus = ..2, data = sys.parent())
##
## Fitting method: "nlminb"
##
##
## Coefficient(s):
## Estimate Std. Error t value Pr(>|t|)
## eta.mu 4.7638777 0.0183385 259.7751 < 2.22e-16 ***
## eta.sigma 2.3461470 0.1473029 15.9274 < 2.22e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Degrees of Freedom for the fit: 2 Residual Deg. of Freedom 28
## Global Deviance: 237.974
## AIC: 241.974
## SBC: 244.777
## *******************************************************************
## Family: c("WEI3", "Weibull type 3")
##
## Call: gamlssML(formula = y, family = DIST[i], parallel = "multicore",
## ncpus = ..2, data = sys.parent())
##
## Fitting method: "nlminb"
##
##
## Coefficient(s):
## Estimate Std. Error t value Pr(>|t|)
## eta.mu 4.2182511 0.0210907 200.0050 < 2.22e-16 ***
## eta.sigma 2.3472225 0.1452416 16.1608 < 2.22e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Degrees of Freedom for the fit: 2 Residual Deg. of Freedom 28
## Global Deviance: 206.524
## AIC: 210.524
## SBC: 213.326
## *******************************************************************
## Family: c("GG", "generalised Gamma Lopatatsidis-Green")
##
## Call: gamlssML(formula = y, family = DIST[i], parallel = "multicore",
## ncpus = ..2, data = sys.parent())
##
## Fitting method: "nlminb"
##
##
## Coefficient(s):
## Estimate Std. Error t value Pr(>|t|)
## eta.mu 4.79623e+00 7.89645e-04 6073.912 < 2.22e-16 ***
## eta.sigma -4.79344e+00 9.02125e-02 -53.135 < 2.22e-16 ***
## eta.nu -2.11826e+03 6.10296e-01 -3470.870 < 2.22e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Degrees of Freedom for the fit: 3 Residual Deg. of Freedom 27
## Global Deviance: 241.279
## AIC: 247.279
## SBC: 251.482
## *******************************************************************
## Family: c("GA", "Gamma")
##
## Call: gamlssML(formula = y, family = DIST[i], parallel = "multicore",
## ncpus = ..2, data = sys.parent())
##
## Fitting method: "nlminb"
##
##
## Coefficient(s):
## Estimate Std. Error t value Pr(>|t|)
## eta.mu 4.9003987 0.0196712 249.1152 < 2.22e-16 ***
## eta.sigma -2.2279998 0.1288503 -17.2914 < 2.22e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Degrees of Freedom for the fit: 2 Residual Deg. of Freedom 28
## Global Deviance: 245.247
## AIC: 249.247
## SBC: 252.05
## *******************************************************************
## Family: c("WEI3", "Weibull type 3")
##
## Call: gamlssML(formula = y, family = DIST[i], parallel = "multicore",
## ncpus = ..2, data = sys.parent())
##
## Fitting method: "nlminb"
##
##
## Coefficient(s):
## Estimate Std. Error t value Pr(>|t|)
## eta.mu 4.560228 0.015302 298.0152 < 2.22e-16 ***
## eta.sigma 2.671412 0.134667 19.8372 < 2.22e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Degrees of Freedom for the fit: 2 Residual Deg. of Freedom 28
## Global Deviance: 205.687
## AIC: 209.687
## SBC: 212.489
## *******************************************************************
## Family: c("IGAMMA", "Inverse Gamma")
##
## Call: gamlssML(formula = y, family = DIST[i], parallel = "multicore",
## ncpus = ..2, data = sys.parent())
##
## Fitting method: "nlminb"
##
##
## Coefficient(s):
## Estimate Std. Error t value Pr(>|t|)
## eta.mu 4.7322002 0.0133658 354.0517 < 2.22e-16 ***
## eta.sigma -2.6196653 0.1289854 -20.3098 < 2.22e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Degrees of Freedom for the fit: 2 Residual Deg. of Freedom 28
## Global Deviance: 212.418
## AIC: 216.418
## SBC: 219.22
## *******************************************************************
## Family: c("WEI3", "Weibull type 3")
##
## Call: gamlssML(formula = y, family = DIST[i], parallel = "multicore",
## ncpus = ..2, data = sys.parent())
##
## Fitting method: "nlminb"
##
##
## Coefficient(s):
## Estimate Std. Error t value Pr(>|t|)
## eta.mu 4.6869925 0.0115867 404.5133 < 2.22e-16 ***
## eta.sigma 2.9681738 0.1416353 20.9565 < 2.22e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Degrees of Freedom for the fit: 2 Residual Deg. of Freedom 28
## Global Deviance: 196.772
## AIC: 200.772
## SBC: 203.574
## *******************************************************************
## Family: c("WEI3", "Weibull type 3")
##
## Call: gamlssML(formula = y, family = DIST[i], parallel = "multicore",
## ncpus = ..2, data = sys.parent())
##
## Fitting method: "nlminb"
##
##
## Coefficient(s):
## Estimate Std. Error t value Pr(>|t|)
## eta.mu 4.9161180 0.0261003 188.355 < 2.22e-16 ***
## eta.sigma 2.1220452 0.1427643 14.864 < 2.22e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Degrees of Freedom for the fit: 2 Residual Deg. of Freedom 28
## Global Deviance: 260.844
## AIC: 264.844
## SBC: 267.646
scan_list
package purrr cung cấp nhiều chiêu thức lợi hại khi lập trình hàm.
Các bạn nên thay thế vòng lặp for loop bằng hàm map.
Việc sử dụng hàm map không chỉ rút gọn cú pháp của vòng lặp mà còn nâng cao tốc độ tính toán khi làm việc với dữ liệu lớn. Một ưu điểm khác mà ít người nhận ra : hàm map cho phép đồng bộ với các « pipelines » của những packages thuộc hệ sinh thái tidyverse (dplyr, broom, ggplot2…) cũng như tương thích với những packages của bộ đôi H. Wickham-Max Kuhn như recipes, rsamples… , cho phép thực hiện quy trình liên tục, trôi chảy.
Tạm biệt các bạn nhé …và hẹn gặp lại !