library(pacman)
p_load(tidyverse,ggplot2,GGally,readxl,ggfortify, magrittr)
#GGally: package for describe correlation between more than 2 variables (viec3)
#ggfortify: package for function autoplot()

#Việc 1: Đọc dữ liệu nghiên cứu tim mạch ‘Framingham dataset.csv’ và gọi đối tượng ‘fmh’. Có những biến số liên quan đến bệnh tim mạch. Các biến liên quan: #‘age’: Tuổi #‘sex’: Giới tính (1/2) #‘sysbp’: systolic blood pressure #‘diasbp’: diastolic blood pressure #‘tot.chol’: Total cholesterol #‘heart.rate’: Nhịp tim #‘bmi’: Tỷ trọng cơ thể #‘smoker’: Hút thuốc lá (0/1) #‘diabetes’: Tiểu đường (0/1) #‘stroke’: Đột quỵ (0/1) #‘cvd’: Cardiovascular disease (0/1) #‘death’: Tử vong (0/1)

fmh=read.csv("/Users/osx/Desktop/Dataset /Framingham dataset.csv")

#Việc 2:Kiểm tra mối tương quan giữa ‘bmi’ và ‘sysbp’. Thể hiện mối liên quan bằng biểu đồ tương quan (dùng ggplot) #viec 2: Kiểm tra mối tương quan giữa ‘bmi’ và ‘tot.chol’. Thể hiện mối liên quan bằng biểu đồ tương quan (dùng ggplot).

p1= ggplot(data=fmh, aes(bmi, sysbp)) + geom_point(aes(color=bmi)) +labs(title = "Correlation between bmi and sysbp", x="Boday Index Mass", y= "Systolic Blood Pressure") + theme(plot.title = element_text(hjust=0.5)) + geom_jitter(alpha=0.05)
p1
## Warning: Removed 52 rows containing missing values (geom_point).

## Warning: Removed 52 rows containing missing values (geom_point).

p2=ggplot(data=fmh, aes(bmi,tot.chol)) + geom_point(aes(color=bmi)) + ggtitle("Correlation between bmi and tot.chol") + theme(plot.title = element_text(hjust=0.5))
p2
## Warning: Removed 454 rows containing missing values (geom_point).

#Việc 3: Kiểm tra mối tương quan giữa các biến ‘age’, ‘bmi’, ‘sysbp’, ‘diasbp’, ‘tot.chol’, và ‘heart.rate’. Hãy vẽ một biểu đồ tương quan đa biến giữa các biến trên.

ggpairs(fmh[,c("tot.chol","age", "sysbp", "diasbp", "bmi", "heart.rate")]) + theme_bw()
## Warning: Removed 409 rows containing non-finite values (stat_density).
## Warning in ggally_statistic(data = data, mapping = mapping, na.rm = na.rm, :
## Removed 409 rows containing missing values

## Warning in ggally_statistic(data = data, mapping = mapping, na.rm = na.rm, :
## Removed 409 rows containing missing values

## Warning in ggally_statistic(data = data, mapping = mapping, na.rm = na.rm, :
## Removed 409 rows containing missing values
## Warning in ggally_statistic(data = data, mapping = mapping, na.rm = na.rm, :
## Removed 454 rows containing missing values
## Warning in ggally_statistic(data = data, mapping = mapping, na.rm = na.rm, :
## Removed 413 rows containing missing values
## Warning: Removed 409 rows containing missing values (geom_point).
## Warning in ggally_statistic(data = data, mapping = mapping, na.rm = na.rm, :
## Removed 52 rows containing missing values
## Warning in ggally_statistic(data = data, mapping = mapping, na.rm = na.rm, :
## Removed 6 rows containing missing values
## Warning: Removed 409 rows containing missing values (geom_point).
## Warning in ggally_statistic(data = data, mapping = mapping, na.rm = na.rm, :
## Removed 52 rows containing missing values
## Warning in ggally_statistic(data = data, mapping = mapping, na.rm = na.rm, :
## Removed 6 rows containing missing values
## Warning: Removed 409 rows containing missing values (geom_point).
## Warning in ggally_statistic(data = data, mapping = mapping, na.rm = na.rm, :
## Removed 52 rows containing missing values
## Warning in ggally_statistic(data = data, mapping = mapping, na.rm = na.rm, :
## Removed 6 rows containing missing values
## Warning: Removed 454 rows containing missing values (geom_point).
## Warning: Removed 52 rows containing missing values (geom_point).

## Warning: Removed 52 rows containing missing values (geom_point).

## Warning: Removed 52 rows containing missing values (geom_point).
## Warning: Removed 52 rows containing non-finite values (stat_density).
## Warning in ggally_statistic(data = data, mapping = mapping, na.rm = na.rm, :
## Removed 54 rows containing missing values
## Warning: Removed 413 rows containing missing values (geom_point).
## Warning: Removed 6 rows containing missing values (geom_point).

## Warning: Removed 6 rows containing missing values (geom_point).

## Warning: Removed 6 rows containing missing values (geom_point).
## Warning: Removed 54 rows containing missing values (geom_point).
## Warning: Removed 6 rows containing non-finite values (stat_density).

#cong thuc ham ggpairs (data) + theme_bw()
#ham ggpairs mac dinh dung Pearson's, duong cheo trong ket qua chinh la distribution cua moi bien

#Việc 4: Đọc dữ liệu ‘Insurance dataset.xlsx’ vào R và gọi đối tượng là ‘ins’ (dùng hàm ‘read_excel’ trong package ‘readxl’) #‘age’ Tuổi #‘sex’ Giới tính (male / female) #‘bmi’ Tỷ trọng cơ thể #‘children’ Số con #‘smoker’ Hút thuốc lá (yes / no) #‘region’ Vùng miền (northeast / northwest / southeast / southwest) #‘charge’ Tiền bảo hiểm ($)

ins= read_excel("/Users/osx/Desktop/Dataset /Insurance dataset.xlsx")
# Dùng mô hình hồi quy tuyến tính qua hàm ‘lm’ để đánh giá mối liên quan giữa ‘age’ và ‘charge’
m=lm(log(charge) ~ age, data=ins)
summary(m)
## 
## Call:
## lm(formula = log(charge) ~ age, data = ins)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -1.3433 -0.4166 -0.3094  0.5000  2.1999 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 7.744247   0.063336  122.27   <2e-16 ***
## age         0.034545   0.001521   22.71   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.7813 on 1336 degrees of freedom
## Multiple R-squared:  0.2786, Adjusted R-squared:  0.2781 
## F-statistic:   516 on 1 and 1336 DF,  p-value: < 2.2e-16
# intercept: 3165
# slope: 257.7
# R2: 0.09
# MSE:133440979 

#Kiểm tra các giả định của mô hình hồi quy tuyến tính
autoplot(m)

#ham gop 4 cua so plot(m) vao 1 window
par(mfrow= c(2,2))
plot(m)

# tinh Mean Square Error (MSE)
#MSE trung binh phuong sai, trong mo hinh hoi quy, no la phuong sai cua Y sau khi hieu chinh cho X trong mô hình 
# phuong sai cua Y: Var(Y) (la phuong sai của Y chua hieu chinh)
# khac biet giua MSE va Var(Y) chinh la phan tram ma mo hinh cua minh giai thich duoc

MSE2=mean(m$residual^2)
MSE2
## [1] 0.6095021
# cho ra nhieu gia tri MSE3 vi moi lan tien luong predict(m) cho ra nhieu gia tri
MSE3=((ins$charge - predict(m))^2) 
MSE3
##          1          2          3          4          5          6          7 
##  284817042    2948729   19720265  482926396   14884207   14046053   67753580 
##          8          9         10         11         12         13         14 
##   52889012   40926577  835980071    7358811  772775453    3306230  122789426 
##         15         16         17         18         19         20         21 
## 1568404004    3344643  116376534    5696016  112205425 1356352142  174742754 
##         22         23         24         25         26         27         28 
##   17147512    1273839 1420759084   38376530  195757914  208568896  150282787 
##         29         30         31         32         33         34         35 
##    7654371 1497859112 1265728035    4795328   21896751  189342480 2619990998 
##         36         37         38         39         40         41         42 
##    2614796  243431993    5260865 1581280911 2319726975    9226337   24412923 
##         43         44         45         46         47         48         49 
##   39229135   39749703   36852361  425210764   11458159   12589800  159266413 
##         50         51         52         53         54         55         56 
## 1497704561    4852172   12754605  555020336 1423823650   64811404 2254991101 
##         57         58         59         60         61         62         63 
##  184895294 1176133385  539875219   35767635   73908198   20214506  909424325 
##         64         65         66         67         68         69         70 
##   17015049  216187285    3009578  202356898   40707613   34939661  311678994 
##         71         72         73         74         75         76         77 
##  274535159   46112830  137643364  142489057   59561192  128753219   15513098 
##         78         79         80         81         82         83         84 
##    2322576    7543354   43058056   19646152   62821355 1380617350  121534293 
##         85         86         87         88         89         90         91 
## 1586229484  444756688 1898277461  122400971   64277633  122609845    4074360 
##         92         93         94         95         96         97         98 
##  119520801  910539936   32718995 2235502404   14123859  146306208  104379732 
##         99        100        101        102        103        104        105 
##  501893053  250008024   38154914   13222742  455245406  956809676   24949368 
##        106        107        108        109        110        111        112 
##  308070759    5396879   14966544    8170299 2213289562  116978155  140937588 
##        113        114        115        116        117        118        119 
##   21508601    5742082  131762307  915073306  129312776  364773082   73820614 
##        120        121        122        123        124        125        126 
##   44587789   59769486    2880686    5058181 1563983422  102123980   11402486 
##        127        128        129        130        131        132        133 
##  291476382   92640575 1070947656   36885556  163984993  185137035  124411557 
##        134        135        136        137        138        139        140 
##    2637908    5996504    4610370    1570113    4150106  746006752    4657947 
##        141        142        143        144        145        146        147 
##  748951923   12123674  359617223  329390918  430031816   26311879 1657403249 
##        148        149        150        151        152        153        154 
##   97379430  119905118    3363990   26176141   60532019   40011873  398224022 
##        155        156        157        158        159        160        161 
##   49957519   48155780  450045395  240554336 1364672656  389664121  455374724 
##        162        163        164        165        166        167        168 
## 1306180370  109013276   26453373   25191608  108112539   23253563   37453761 
##        169        170        171        172        173        174        175 
##    7348866   23224958  179438609   65727390    2844047   27427150    8104636 
##        176        177        178        179        180        181        182 
## 2382858282   41561304  108711617   77685638   72746022  137502153    2635607 
##        183        184        185        186        187        188        189 
##   15975356   54911267   59631799 1913568085   15787386   28269111   45789588 
##        190        191        192        193        194        195        196 
##   24149065  157446125   23764437    4532836  144833119    1274875    2660691 
##        197        198        199        200        201        202        203 
##   31816633   72378070   92828339  221758606    4504052   78530600  169062190 
##        204        205        206        207        208        209        210 
## 1378282052   50947785   18740446  137675412  439956509  192405429   43572985 
##        211        212        213        214        215        216        217 
##    3887344   66481033   12454756   24938677   72432478   54208047  107041079 
##        218        219        220        221        222        223        224 
##    6125917   11453719  628665083   25035535  111414556   27506609 1209037351 
##        225        226        227        228        229        230        231 
##  380497552  143461080    7186602  586491635   54008022   84932601   55271016 
##        232        233        234        235        236        237        238 
##  195762192    2956283  151882100   44904746  377724657    2583737   19841590 
##        239        240        241        242        243        244        245 
##  300824038   51028266 1482487902   28571061 1235556971   51663620  871031631 
##        246        247        248        249        250        251        252 
##  598627670  159741451    3913909    3325858   16255510  164380324 2236853386 
##        253        254        255        256        257        258        259 
## 1958163410   18080074 1688198290  169981312 1928220265   29072839  132493770 
##        260        261        262        263        264        265        266 
## 1138515221  139887678  291618207  618034327 1311236872  418342493 2129064893 
##        267        268        269        270        271        272        273 
##  294822497  212601176   55230971   85989338    2927761 1835896816   52657400 
##        274        275        276        277        278        279        280 
##   92317333    6323020   94215621    7813687    4588013  166900786   96936339 
##        281        282        283        284        285        286        287 
##  498291361 2356089726   17878909  140884076   92474753   59795832   88804086 
##        288        289        290        291        292        293        294 
##  202956272 2293165625  675130863   10006508  410834852 1772715468    4614968 
##        295        296        297        298        299        300        301 
##   15190655    2877101  265346031  482650539 1500597002   85379315   45397337 
##        302        303        304        305        306        307        308 
##  618209036  150205323   18842115  159680973  377665098  406786932   17158219 
##        309        310        311        312        313        314        315 
##  142440561   59907535   71149267    2989356 1773697282   65852761 1213142556 
##        316        317        318        319        320        321        322 
##   94346816   77894630  108690124   54936711   21704026   23871379  608264601 
##        323        324        325        326        327        328        329 
## 1259023505  133554708    8164420   43442334   12625315 1827665394 2296141899 
##        330        331        332        333        334        335        336 
##   83448671 2352998090  594583446  180074497  135692220  366161498  190794766 
##        337        338        339        340        341        342        343 
##  147203905  193983068 1756416709   67622753  358975426  178014668  174432180 
##        344        345        346        347        348        349        350 
##  195214821  120291964   38135326   23825286   69307693   29910496    2648213 
##        351        352        353        354        355        356        357 
##  139733532   79613014   12570020  153660689  199506350  604850820   79830325 
##        358        359        360        361        362        363        364 
##   92370607    3344301    2557262  100678077   22487671  191437813    6704523 
##        365        366        367        368        369        370        371 
##   10061840   95431607  180112135   64125918   65723524   12065216  179699028 
##        372        373        374        375        376        377        378 
##  144470144   58220292 1301519378    1912948  324916095  468758812 1452957008 
##        379        380        381        382        383        384        385 
##  270462782  728519408  224937079 1788786490  431469532   34081827   68778351 
##        386        387        388        389        390        391        392 
##    1571158  140343439  916586006   10037323   21247551  115078577    4535495 
##        393        394        395        396        397        398        399 
##   80185902   86131440   88391430   56510899   72467309  274831013  224354769 
##        400        401        402        403        404        405        406 
##    2635110   85660411   65198389  215582097  105268073   10571497  129671959 
##        407        408        409        410        411        412        413 
##   17440761   72766072   44135718   16529136    2601574  383593589  208715856 
##        414        415        416        417        418        419        420 
##   25719992    4522006   53824187   83382656  345932998  207599837  837555354 
##        421        422        423        424        425        426        427 
## 2197669337 2170558859 1530077587    7391804   80262384   95639933   42850291 
##        428        429        430        431        432        433        434 
##   53514620    9979194  353292452  532435076   23987109   35527892  159475932 
##        435        436        437        438        439        440        441 
##   17933318  193488265    5045830   35021454  158331736    8344478   22367726 
##        442        443        444        445        446        447        448 
## 1374221063    1301949  799651802  681191745   53813745  161828488  130972980 
##        449        450        451        452        453        454        455 
##   34831455   22594580   56297642   16188231    3845681    3101461   21879371 
##        456        457        458        459        460        461        462 
##  474682868  140952110  139974022  112184813   58883277  107579250  489951003 
##        463        464        465        466        467        468        469 
##  231661733  124450510    2636193  380764490  174632949  159610351  541968498 
##        470        471        472        473        474        475        476 
##    4808069    6191942    4818489    3013920  435532548  643773262  832831053 
##        477        478        479        480        481        482        483 
## 1234746169    6379594    2328172    3296936  241655362   86401950    2604423 
##        484        485        486        487        488        489        490 
##   97427993   91271781   18820050  155394714    1551358 2388850803  109252755 
##        491        492        493        494        495        496        497 
##    3028900  600408759    4787813  157859059  321615311    3836065   24234273 
##        498        499        500        501        502        503        504 
##   64299056   67270113  181197820 1309640315   46623482  493222307 1058847688 
##        505        506        507        508        509        510        511 
##   35585135   46074783    6941983    9416464    9215110  131000997  138140581 
##        512        513        514        515        516        517        518 
##    6198792   87456551    1557250  444074216  128890771  768168541   70629977 
##        519        520        521        522        523        524        525 
##   27371853   14814371  657773931   15882840   97156483   29036576 1462022142 
##        526        527        528        529        530        531        532 
##  131658844  578464050   97052426   69448479    2888760 2368360516  196941667 
##        533        534        535        536        537        538        539 
##  166825732  368858264  191024466   36700210   35563258   77717495   67630297 
##        540        541        542        543        544        545        546 
##  747282424   38285518    9290017  192578995 4065441954  104487040  566335458 
##        547        548        549        550        551        552        553 
##   10628481  132913490   10272116 2102587052  179041484   15713891  167630815 
##        554        555        556        557        558        559        560 
##  124950280  319347365   14737633   69309892   15415526 1597958462    2683139 
##        561        562        563        564        565        566        567 
##   84355135  119122458    6176940   81889085    7800250    4494529   40506865 
##        568        569        570        571        572        573        574 
##   52527162  133251631 2087815517   14081083    4888871   22513660  999199371 
##        575        576        577        578        579        580        581 
##  174618880  149161044    2743977 3429538218   94381019   10226459  166518635 
##        582        583        584        585        586        587        588 
##    2660691   40286684  310372424    1523781   22759751   14843605 1930280475 
##        589        590        591        592        593        594        595 
##  185662055   35614328  140012651   70874534    6543546  235641989   32490043 
##        596        597        598        599        600        601        602 
##   77698097   58233902   31202577   55238657 1119734308    2639580   83990436 
##        603        604        605        606        607        608        609 
##  122342540  258411167  304871969   86008114   12602097  658897358   19592864 
##        610        611        612        613        614        615        616 
## 1539201720   72902967   43066236    4837059   45483144    3503017 1845603257 
##        617        618        619        620        621        622        623 
##  135686068  542755336 1185525114  114575611   13326628 1613887893   84139031 
##        624        625        626        627        628        629        630 
## 1197815731  146890321   13895886   45422253  128093290  128963372 1846781373 
##        631        632        633        634        635        636        637 
##  101531234    3877913   11275651   51326247   88018921  207388138    7293842 
##        638        639        640        641        642        643        644 
##  620313465  405628921  167430055   44320426 1074414555  172502307   19874501 
##        645        646        647        648        649        650        651 
##  353324043  102652030   37386833   67949658    2903142  154286346   95872517 
##        652        653        654        655        656        657        658 
##  111727772   68413085   72560297  149689073  608010967   11571438   16402787 
##        659        660        661        662        663        664        665 
##  696055190  206919158   41301204  492073235   26416546    1272459  730510566 
##        666        667        668        669        670        671        672 
## 1810604682   75585967 1599536529 2088519413   42134507   23317326   15483039 
##        673        674        675        676        677        678        679 
##   19278625   38144955 2133675077   52034402  155654485 2127119849  152618060 
##        680        681        682        683        684        685        686 
##  102968636    6639895    1522409 1607592866   97099279   22630360  126220771 
##        687        688        689        690        691        692        693 
##   59605358   29480806  687866655 1210886240    4391723   64944534    5540346 
##        694        695        696        697        698        699        700 
##    5496188   12740060   10192712  851291915 1621228744  120265591   12194577 
##        701        702        703        704        705        706        707 
##    4048476   90863295   90149996   28905883   79594352   28795606 1970552034 
##        708        709        710        711        712        713        714 
##  105165130   37263894   29811805    2955559  101964537   68916720    3904648 
##        715        716        717        718        719        720        721 
##    5997252  147310508   91346837  171683049  117475862  149374002   97341396 
##        722        723        724        725        726        727        728 
##  126674257  168207203    1574644  101942599 1674745039   44296018  277188248 
##        729        730        731        732        733        734        735 
##    4880720   45862606  374540369  101119875   17862065   89072974  195928315 
##        736        737        738        739        740        741        742 
##   91670214 1632967832   12080411 1309033133 1987083058   73877203  332618025 
##        743        744        745        746        747        748        749 
## 1870116430   14055223   77752510   98027590  137567800    2620778   73060425 
##        750        751        752        753        754        755        756 
##    9325675  381429516    3601981  201656495  139807790  293089358   25225048 
##        757        758        759        760        761        762        763 
##   63628118  531581572   29375827 1317648781   15344879    5800469  362216710 
##        764        765        766        767        768        769        770 
##    9376651   82551207  140017769   64857189   49581974  204749652   47944342 
##        771        772        773        774        775        776        777 
##  780165121  124126446  163531547  314711347   52599922  111323298   48686496 
##        778        779        780        781        782        783        784 
##   55340286   35110679   97224235  333078392    1296024   87921664  600778949 
##        785        786        787        788        789        790        791 
##   18850352   41026903  162087285    3643703   27048662  180850715   31957918 
##        792        793        794        795        796        797        798 
##    1547552    7416951  448856887   51845764  334965585   18125329   22191104 
##        799        800        801        802        803        804        805 
##  140147552  320254032   49526787  204601302    4387392 1504223442    3266467 
##        806        807        808        809        810        811        812 
##   59637919  810404759    4530434    1261445   10897821   88465061   40349293 
##        813        814        815        816        817        818        819 
##  121090266   19537959   31084169    3495009    8032618   12881332  547182758 
##        820        821        822        823        824        825        826 
## 3038932971   55303305    7141224    2603436   67403109  156594886  257895644 
##        827        828        829        830        831        832        833 
## 1918806827  431170269 1567229269   37312587  179127050   27640021   22193757 
##        834        835        836        837        838        839        840 
##  137691127   28820469   51138734   19300603  135676839   40874771  159072560 
##        841        842        843        844        845        846        847 
##    2303845  151638379 1296898169  757581563  101253502 2024922470   97282615 
##        848        849        850        851        852        853        854 
##    5902550    8793888  112190210 1388391718  199085567 1772606307  137360846 
##        855        856        857        858        859        860        861 
##  580688330    3485349 1678111766  249936427  331587839  120028079 2125623868 
##        862        863        864        865        866        867        868 
##   51008664  150308689   29692239   76964879   43444378    1283868  133781995 
##        869        870        871        872        873        874        875 
##  172127891   19211076   71374559   11449580   35494097   46782814   78887708 
##        876        877        878        879        880        881        882 
##    7190845  682825155   44154754   39351462   39726921   11796201    7731284 
##        883        884        885        886        887        888        889 
##    6642893 2138656116   23710795  388517411  740314658   27701147    2802587 
##        890        891        892        893        894        895        896 
##  142454236  859722684   52342703  108436962 1953046504  183470245  170407738 
##        897        898        899        900        901        902        903 
##  391597589    4898094    2644550    4447621   75332364 2368159770   21647096 
##        904        905        906        907        908        909        910 
##   65875097  159637465   20753027   23408597   58132335  229830100  305811128 
##        911        912        913        914        915        916        917 
##    6919734 1137329803  206581020   58029792   27548051    6075046  473719250 
##        918        919        920        921        922        923        924 
## 1229208911  169834835   27418561  180666824  180973360   30021688   18588964 
##        925        926        927        928        929        930        931 
##   38952644  641297953    8440003  144543216  181196323   39445865    8517189 
##        932        933        934        935        936        937        938 
##   38803014  101760414   53858620   21757957  149427291 1030371390   80222551 
##        939        940        941        942        943        944        945 
##    5269945   89833793    1239900   91013386    4880136    2624628  168298435 
##        946        947        948        949        950        951        952 
##  136059422   51135354 1523985946   40317183  396999652  132829298 2251824028 
##        953        954        955        956        957        958        959 
##   20415960 1520164093  400028595   14953062 1763152568  158793107 1683049945 
##        960        961        962        963        964        965        966 
##  809944085    7407690   11185934  209229120   90083632  700002311   22442870 
##        967        968        969        970        971        972        973 
##  573979870   56381494   10702050   73749215  114343904   24834850    6346945 
##        974        975        976        977        978        979        980 
##    3065782    5354501  260177416   60758254    8376765   94000185   23814878 
##        981        982        983        984        985        986        987 
##  650632758   20172858  368299427  281828046   24074966   57993796   70573729 
##        988        989        990        991        992        993        994 
##  802639332   20339265  212090216   11358704   50919723  102190921   29980118 
##        995        996        997        998        999       1000       1001 
##  269347756   63638651   54899660  192600214   42809094   27655296  301126106 
##       1002       1003       1004       1005       1006       1007       1008 
## 1187785746    3858776  450406384   74272910   19577454   19620930  620301513 
##       1009       1010       1011       1012       1013       1014       1015 
##  539766093   98966993   68221679  351881623 1337396399   76664849   28885025 
##       1016       1017       1018       1019       1020       1021       1022 
##  146778313    7294555   15834698  155892234  676545113   77248049 1266440659 
##       1023       1024       1025       1026       1027       1028       1029 
## 1780989419    2899054   73282056    4046966  270355612  465991813   96841784 
##       1030       1031       1032       1033       1034       1035       1036 
##   47182580  469500063 1972617499   17046511  188774033  167449286  146044042 
##       1037       1038       1039       1040       1041       1042       1043 
## 1404446458 1577378073    5028048  505586874  409087088    2877549 1120065658 
##       1044       1045       1046       1047       1048       1049       1050 
##    9939785  129605049  478366463   53521201 1979617613   12856262 1577533588 
##       1051       1052       1053       1054       1055       1056       1057 
##   64222132  206916797   86092228  640096129   11187642  112039205   68361818 
##       1058       1059       1060       1061       1062       1063       1064 
##  321126565    6112790   19836977    3892763  133275718 2397127152   42761231 
##       1065       1066       1067       1068       1069       1070       1071 
##   32491379   49509572   80439063   33043205  205633702  119229787 1589033407 
##       1072       1073       1074       1075       1076       1077       1078 
##  195008237    3614021  146096573  174094004   20738847   72965407    4383978 
##       1079       1080       1081       1082       1083       1084       1085 
## 1201553774  229571401  141031835   19762941   34185603   16545755  225296318 
##       1086       1087       1088       1089       1090       1091       1092 
##  361538603  116353027  128675316   94855328  111670117 1736114992  127168349 
##       1093       1094       1095       1096       1097       1098       1099 
##   12837468 1149145168  127461198   20728192 1991987873    2775983  530658474 
##       1100       1101       1102       1103       1104       1105       1106 
##   10358827  281146367  126424070   11990039  128902765  416632689  106694891 
##       1107       1108       1109       1110       1111       1112       1113 
##   80617445  109924201    8383605   73892294  132314300 1758979297  584262915 
##       1114       1115       1116       1117       1118       1119       1120 
##   28126670    5700429  116593393   84883828 1304362987 1464888762   32315244 
##       1121       1122       1123       1124       1125       1126       1127 
## 1166750807   69519425 2176396719  357014013 1672455065  202911125  104141857 
##       1128       1129       1130       1131       1132       1133       1134 
##   33960538  205906589    2960108   73499943   13577390  428461311   99630283 
##       1135       1136       1137       1138       1139       1140       1141 
##  386697446  122680330   57976860   10033977   13656500 1360896630   81695491 
##       1142       1143       1144       1145       1146       1147       1148 
##   63128688  734868234   40056040   92562207  127228669 2764762869    5076611 
##       1149       1150       1151       1152       1153       1154       1155 
##  116258333   35647298    4819649  149477309 1675433185   31601313  121127024 
##       1156       1157       1158       1159       1160       1161       1162 
##   52117250 1577229246  207865317    6008798   15848292   59567887   26165986 
##       1163       1164       1165       1166       1167       1168       1169 
##  359268950    4806902   51042356   27238331  120402076   20437008   21732289 
##       1170       1171       1172       1173       1174       1175       1176 
##   37250644  294819763  504872602  122853817   41586847   19582128    4604701 
##       1177       1178       1179       1180       1181       1182       1183 
##  570164715   42091029    8357595  374095702   58393724    8077592    6887392 
##       1184       1185       1186       1187       1188       1189       1190 
##   89075466  335611383   73865853 1403020002  191404767  473589545  172085562 
##       1191       1192       1193       1194       1195       1196       1197 
##   28287348  188137194  169244831   75026586   17017830  354580313 1108833404 
##       1198       1199       1200       1201       1202       1203       1204 
##   32383804   40761552   24264391   38312750   76106360    4189475   99093144 
##       1205       1206       1207       1208       1209       1210       1211 
##  331789327   26087037 1361670931 1475058177  411596494  152211182   28776534 
##       1212       1213       1214       1215       1216       1217       1218 
##  554787368    2869938  116581653   15581923  165937978   29230622   16397398 
##       1219       1220       1221       1222       1223       1224       1225 
## 1734911486   56672394   22178665   43354090   71118786  682110205   46913171 
##       1226       1227       1228       1229       1230       1231       1232 
##   22913192   43976632   51162792  112032634  142289305 3601423146  406381285 
##       1233       1234       1235       1236       1237       1238       1239 
##  155503376  128499707   72359860    7241082  208511688  149196526   48671336 
##       1240       1241       1242       1243       1244       1245       1246 
##   10431788 2233537223 2456957603   18384946   10003958    1271425   31434608 
##       1247       1248       1249       1250       1251       1252       1253 
##   82673542   36605994    2642562 1413660588  347443947    1520694  263231540 
##       1254       1255       1256       1257       1258       1259       1260 
##  250262771   19414951   41793870  130581633  127606955  903239088  103800063 
##       1261       1262       1263       1264       1265       1266       1267 
##   20569718   10682762   45711552   53707181  107360176  724501163  114379298 
##       1268       1269       1270       1271       1272       1273       1274 
## 1172752902    3504578   74063257   10783917    9079382  209354868   22449588 
##       1275       1276       1277       1278       1279       1280       1281 
##  290180969  119894105    7471715   18906791  504135064   17476624   68463681 
##       1282       1283       1284       1285       1286       1287       1288 
##  601540897  203778290    2930902 2246193937   72680807   13867532   29849408 
##       1289       1290       1291       1292       1293       1294       1295 
## 1469658929   50954022   50763424 1212450047    2270673   86351676  142119239 
##       1296       1297       1298       1299       1300       1301       1302 
##    3827285    2891903   18763880   27589651    7303117 3916703774 2181663159 
##       1303       1304       1305       1306       1307       1308       1309 
##   10241146 1430389805  451570269    6032159  259421225  460687375 1148670723 
##       1310       1311       1312       1313       1314       1315       1316 
##   47152948   48048669   20816669   20496809 1324172086  351828607  126876915 
##       1317       1318       1319       1320       1321       1322       1323 
##    2969682    1334248  379773018   51733629   29335311  789129395  168258766 
##       1324       1325       1326       1327       1328       1329       1330 
## 1926084670   17902017  172488432   49573233   87766893  501186968  106412953 
##       1331       1332       1333       1334       1335       1336       1337 
##  159250576  116367967  130008897  112170908    4829511    2629157    3997901 
##       1338 
##  848644805
# co the dung ham anova
MSE1= anova(m)

#Việc 5: Mô phỏng và lấy mẫu. Trong code dưới đây chúng ta sẽ mô phỏng một dân số (population) gồm 100 000 người (phut 2:16)

#việc 6: lựa chọn biến để thao tác trong data

# goi ten cac bien trong data fmh
names(fmh)
##  [1] "id"           "sex"          "tot.chol"     "age"          "sysbp"       
##  [6] "diasbp"       "smoker"       "cigs.day"     "bmi"          "diabetes"    
## [11] "bpmed"        "heart.rate"   "glucose"      "educ"         "prev.chd"    
## [16] "prev.ap"      "prev.mi"      "prev.stroke"  "prev.hyp"     "time"        
## [21] "period"       "hdlc"         "ldlc"         "death"        "angina"      
## [26] "hosp.mi"      "mi.fchd"      "any.chd"      "stroke"       "cvd"         
## [31] "hypertension" "time.ap"      "time.mi"      "time.mi.1"    "time.chd"    
## [36] "time.stroke"  "time.cvd"     "time.dth"     "time.hyp"
# lay data trong 3 cot id, sex, age de lam viec
df1 = fmh[, c("id", "sex", "age")] # co dau "" voi id, sex, age hieu ten moi bien hieu la lable
head(df1)
##     id sex age
## 1 2448   1  39
## 2 2448   1  52
## 3 6238   2  46
## 4 6238   2  52
## 5 6238   2  58
## 6 9428   1  48
df2= fmh[, c(1,2,4)] # 1,2,4: so thu tu cua moi bien trong data
head(df2)
##     id sex age
## 1 2448   1  39
## 2 2448   1  52
## 3 6238   2  46
## 4 6238   2  52
## 5 6238   2  58
## 6 9428   1  48
df3= fmh %>% select(id, sex, age) # khong co dau "" voi id, sex, age vi o day la ten bien
head(df3)
##     id sex age
## 1 2448   1  39
## 2 2448   1  52
## 3 6238   2  46
## 4 6238   2  52
## 5 6238   2  58
## 6 9428   1  48
#ket qua df1, df2, df3 nhu nhau