library(dplyr)
## Warning: package 'dplyr' was built under R version 4.3.1
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ forcats   1.0.0     ✔ readr     2.1.4
## ✔ ggplot2   3.4.2     ✔ stringr   1.5.0
## ✔ lubridate 1.9.2     ✔ tibble    3.2.1
## ✔ purrr     1.0.1     ✔ tidyr     1.3.0
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(ggplot2)

#......................probability...............................................

# data importing
deals = readRDS("/Users/abdullahibnmasud/statistics/data/seller_1.rds")
deals
##       product  client status  amount num_users
## 1   Product F Current    Won 7389.52        19
## 2   Product C     New    Won 4493.01        43
## 3   Product B     New    Won 5738.09        87
## 4   Product I Current    Won 2591.24        83
## 5   Product E Current    Won 6622.97        17
## 6   Product B     New    Won 5496.27         2
## 7   Product C Current    Won 3043.13        29
## 8   Product N Current    Won 7340.64        13
## 9   Product F Current    Won 6780.85        80
## 10  Product B Current    Won 5237.24        23
## 11  Product B Current    Won 2273.55        26
## 12  Product C     New   Lost 2480.33         6
## 13  Product D     New   Lost 4179.72        15
## 14  Product F     New    Won 4918.79        43
## 15  Product B Current   Lost 7030.54        41
## 16  Product B Current    Won 4599.65        12
## 17  Product D Current    Won 5282.57        58
## 18  Product N Current    Won 8627.20         7
## 19  Product A Current    Won 9913.27        34
## 20  Product I Current    Won 3220.45        45
## 21  Product F Current   Lost 6849.12        19
## 22  Product B Current    Won 6236.52         2
## 23  Product F Current    Won 5226.04        82
## 24  Product A Current    Won 1625.19        23
## 25  Product B     New   Lost 3124.62        31
## 26  Product C     New    Won 3342.35        16
## 27  Product B Current   Lost 5701.70        53
## 28  Product B Current    Won 4656.29        26
## 29  Product B Current    Won 4563.53        73
## 30  Product B Current   Lost 5867.54         1
## 31  Product C     New    Won 6151.14        66
## 32  Product B Current   Lost 7404.46        13
## 33  Product I Current    Won 3711.23        14
## 34  Product E     New   Lost 3706.78         3
## 35  Product A     New   Lost 5656.77        33
## 36  Product B Current    Won 8588.35        52
## 37  Product N Current    Won 2738.93        32
## 38  Product F Current    Won 3357.24        53
## 39  Product B Current    Won 2812.45        37
## 40  Product I Current    Won 6533.16        47
## 41  Product B Current    Won 7229.87        99
## 42  Product A Current   Lost 5597.34        89
## 43  Product A Current    Won 5827.35        50
## 44  Product D Current    Won 3328.79        40
## 45  Product B Current    Won 7085.83        17
## 46  Product B Current    Won 8614.12        17
## 47  Product B Current    Won 8222.62         2
## 48  Product C Current   Lost 1548.61        65
## 49  Product C Current   Lost 3727.66        19
## 50  Product B Current    Won 3488.36        79
## 51  Product B     New   Lost 8325.78        74
## 52  Product B Current   Lost 1378.80        42
## 53  Product I     New    Won 7153.77        48
## 54  Product A Current    Won 1649.91        12
## 55  Product A Current    Won 6010.04        24
## 56  Product I Current    Won 1606.86        24
## 57  Product D Current    Won 6820.84        42
## 58  Product C Current   Lost 5411.26        17
## 59  Product B Current    Won 6558.74        90
## 60  Product E Current   Lost 6407.69         6
## 61  Product B Current    Won 6251.99         2
## 62  Product D Current    Won 6733.62        27
## 63  Product F Current    Won 5137.67         3
## 64  Product B Current    Won 4171.76        94
## 65  Product B Current    Won 1640.06        59
## 66  Product B     New   Lost 2746.79        50
## 67  Product G     New   Lost 4279.60        51
## 68  Product B Current    Won 4571.26        19
## 69  Product F Current   Lost 4430.53         7
## 70  Product J     New   Lost 3182.09         2
## 71  Product I Current    Won 2588.60        51
## 72  Product A Current    Won 5945.63        55
## 73  Product B Current   Lost 4606.44        78
## 74  Product D     New   Lost 2723.39        42
## 75  Product D Current   Lost 4274.80         9
## 76  Product D Current    Won 9215.41         3
## 77  Product B Current    Won 3950.94        39
## 78  Product B Current    Won 6252.30        27
## 79  Product C Current    Won 1300.89        42
## 80  Product B Current    Won 4866.12        22
## 81  Product B Current    Won 5430.55        38
## 82  Product B Current    Won 2907.38        43
## 83  Product B     New    Won 5665.33        68
## 84  Product B     New   Lost 3219.33         3
## 85  Product G Current   Lost 9062.59        24
## 86  Product B     New   Lost 4735.66         1
## 87  Product B     New   Lost 3690.75        10
## 88  Product C Current    Won 3579.63         3
## 89  Product B Current    Won 5276.01        82
## 90  Product B Current    Won 3231.52        65
## 91  Product D     New   Lost 5744.26        17
## 92  Product A Current    Won 5121.83         2
## 93  Product A Current    Won 4509.96        92
## 94  Product B Current    Won 4216.81        35
## 95  Product D Current   Lost 4736.38        73
## 96  Product J     New   Lost 4269.87        18
## 97  Product A     New   Lost 1998.75        39
## 98  Product E Current   Lost 4823.99        35
## 99  Product F Current    Won 1420.71        33
## 100 Product A Current    Won 5758.67        23
## 101 Product H Current    Won 6201.23        48
## 102 Product H Current    Won 5116.34        63
## 103 Product D     New   Lost 1196.53        63
## 104 Product E Current    Won 2910.08        14
## 105 Product D Current    Won 4110.98        39
## 106 Product H Current    Won  416.55        47
## 107 Product H Current    Won 4547.97        13
## 108 Product H Current    Won 9520.58        99
## 109 Product H Current    Won 3608.69        56
## 110 Product H Current    Won 5257.16        22
## 111 Product B Current    Won  696.88        44
## 112 Product H     New   Lost  787.57        24
## 113 Product D Current    Won 3727.65        12
## 114 Product B Current    Won 3880.07         4
## 115 Product A Current    Won 3371.70        18
## 116 Product F Current   Lost 1664.40        58
## 117 Product B     New   Lost 5287.36        81
## 118 Product D Current   Lost 3416.82        12
## 119 Product D Current   Lost 5592.66        85
## 120 Product B Current    Won 1597.44        37
## 121 Product D Current   Lost 8180.81        96
## 122 Product B Current    Won 4831.73        12
## 123 Product B Current    Won 7219.99        16
## 124 Product B Current   Lost 3021.30         1
## 125 Product C Current    Won  482.01        65
## 126 Product C Current    Won 4796.13        44
## 127 Product D     New   Lost 4861.06        16
## 128 Product B Current    Won 2070.25         7
## 129 Product D Current   Lost 4843.50        80
## 130 Product B Current    Won 6509.25        53
## 131 Product A Current    Won 3425.45        82
## 132 Product B Current    Won 6872.29        25
## 133 Product B     New   Lost 4429.76        90
## 134 Product D Current    Won 5992.86        98
## 135 Product D Current    Won 3976.47        29
## 136 Product D Current    Won 4814.25        67
## 137 Product B Current    Won 2924.29        32
## 138 Product A Current    Won 5145.01        89
## 139 Product C Current    Won -539.23        11
## 140 Product B Current    Won 4566.61         9
## 141 Product B Current   Lost 5856.18        45
## 142 Product B Current    Won 4795.64         8
## 143 Product A Current    Won 3847.71        17
## 144 Product A Current    Won 5736.79        72
## 145 Product B     New    Won 3682.25        89
## 146 Product A Current    Won 4682.94        63
## 147 Product D Current   Lost 5560.03        68
## 148 Product D Current   Lost 7969.67        23
## 149 Product D Current    Won 3485.48        52
## 150 Product D Current    Won 3415.63        36
## 151 Product D Current   Lost 7550.60        17
## 152 Product D Current    Won 6221.36         2
## 153 Product A Current   Lost 9027.17        71
## 154 Product D Current   Lost 5436.98        54
## 155 Product B Current   Lost 6979.77         8
## 156 Product D Current    Won 4509.53         5
## 157 Product B Current    Won 6204.95        58
## 158 Product D Current    Won 5817.77         5
## 159 Product A Current    Won 4665.88        20
## 160 Product B Current    Won 1676.09        29
## 161 Product D Current   Lost 6690.06        60
## 162 Product D Current    Won 4837.09        31
## 163 Product D Current    Won 6755.66        59
## 164 Product C Current    Won 7202.17        31
## 165 Product F     New    Won 7576.35        29
## 166 Product B Current   Lost 4371.48         6
## 167 Product C     New   Lost 3779.86        11
## 168 Product B Current    Won 4965.08         9
## 169 Product A Current    Won 8448.77         8
## 170 Product D Current   Lost 2101.18         5
## 171 Product D Current   Lost 8031.36        59
## 172 Product D Current    Won 1340.15        66
## 173 Product D Current   Lost 3086.88        55
## 174 Product A Current   Lost 5835.32        23
## 175 Product D Current    Won 6377.50        12
## 176 Product D Current    Won 3537.61        28
## 177 Product A Current    Won 6448.07        34
## 178 Product D     New   Lost 7320.05        72
# calculating probability of picking a deal with each product
deals %>% 
  mutate(probabiltity = amount/sum(amount))
##       product  client status  amount num_users  probabiltity
## 1   Product F Current    Won 7389.52        19  0.0086272141
## 2   Product C     New    Won 4493.01        43  0.0052455585
## 3   Product B     New    Won 5738.09        87  0.0066991809
## 4   Product I Current    Won 2591.24        83  0.0030252550
## 5   Product E Current    Won 6622.97        17  0.0077322722
## 6   Product B     New    Won 5496.27         2  0.0064168577
## 7   Product C Current    Won 3043.13        29  0.0035528335
## 8   Product N Current    Won 7340.64        13  0.0085701471
## 9   Product F Current    Won 6780.85        80  0.0079165961
## 10  Product B Current    Won 5237.24        23  0.0061144419
## 11  Product B Current    Won 2273.55        26  0.0026543541
## 12  Product C     New   Lost 2480.33         6  0.0028957683
## 13  Product D     New   Lost 4179.72        15  0.0048797946
## 14  Product F     New    Won 4918.79        43  0.0057426537
## 15  Product B Current   Lost 7030.54        41  0.0082081074
## 16  Product B Current    Won 4599.65        12  0.0053700600
## 17  Product D Current    Won 5282.57        58  0.0061673644
## 18  Product N Current    Won 8627.20         7  0.0100721971
## 19  Product A Current    Won 9913.27        34  0.0115736750
## 20  Product I Current    Won 3220.45        45  0.0037598534
## 21  Product F Current   Lost 6849.12        19  0.0079963008
## 22  Product B Current    Won 6236.52         2  0.0072810945
## 23  Product F Current    Won 5226.04        82  0.0061013660
## 24  Product A Current    Won 1625.19        23  0.0018973982
## 25  Product B     New   Lost 3124.62        31  0.0036479725
## 26  Product C     New    Won 3342.35        16  0.0039021708
## 27  Product B Current   Lost 5701.70        53  0.0066566958
## 28  Product B Current    Won 4656.29        26  0.0054361868
## 29  Product B Current    Won 4563.53        73  0.0053278901
## 30  Product B Current   Lost 5867.54         1  0.0068503129
## 31  Product C     New    Won 6151.14        66  0.0071814139
## 32  Product B Current   Lost 7404.46        13  0.0086446565
## 33  Product I Current    Won 3711.23        14  0.0043328357
## 34  Product E     New   Lost 3706.78         3  0.0043276403
## 35  Product A     New   Lost 5656.77        33  0.0066042403
## 36  Product B Current    Won 8588.35        52  0.0100268400
## 37  Product N Current    Won 2738.93        32  0.0031976821
## 38  Product F Current    Won 3357.24        53  0.0039195548
## 39  Product B Current    Won 2812.45        37  0.0032835162
## 40  Product I Current    Won 6533.16        47  0.0076274197
## 41  Product B Current    Won 7229.87        99  0.0084408238
## 42  Product A Current   Lost 5597.34        89  0.0065348562
## 43  Product A Current    Won 5827.35        50  0.0068033913
## 44  Product D Current    Won 3328.79        40  0.0038863396
## 45  Product B Current    Won 7085.83        17  0.0082726581
## 46  Product B Current    Won 8614.12        17  0.0100569263
## 47  Product B Current    Won 8222.62         2  0.0095998527
## 48  Product C Current   Lost 1548.61        65  0.0018079916
## 49  Product C Current   Lost 3727.66        19  0.0043520176
## 50  Product B Current    Won 3488.36        79  0.0040726365
## 51  Product B     New   Lost 8325.78        74  0.0097202913
## 52  Product B Current   Lost 1378.80        42  0.0016097396
## 53  Product I     New    Won 7153.77        48  0.0083519776
## 54  Product A Current    Won 1649.91        12  0.0019262587
## 55  Product A Current    Won 6010.04        24  0.0070166807
## 56  Product I Current    Won 1606.86        24  0.0018759981
## 57  Product D Current    Won 6820.84        42  0.0079632841
## 58  Product C Current   Lost 5411.26        17  0.0063176091
## 59  Product B Current    Won 6558.74        90  0.0076572842
## 60  Product E Current   Lost 6407.69         6  0.0074809343
## 61  Product B Current    Won 6251.99         2  0.0072991556
## 62  Product D Current    Won 6733.62        27  0.0078614554
## 63  Product F Current    Won 5137.67         3  0.0059981946
## 64  Product B Current    Won 4171.76        94  0.0048705013
## 65  Product B Current    Won 1640.06        59  0.0019147588
## 66  Product B     New   Lost 2746.79        50  0.0032068586
## 67  Product G     New   Lost 4279.60        51  0.0049964038
## 68  Product B Current    Won 4571.26        19  0.0053369148
## 69  Product F Current   Lost 4430.53         7  0.0051726135
## 70  Product J     New   Lost 3182.09         2  0.0037150683
## 71  Product I Current    Won 2588.60        51  0.0030221728
## 72  Product A Current    Won 5945.63        55  0.0069414824
## 73  Product B Current   Lost 4606.44        78  0.0053779872
## 74  Product D     New   Lost 2723.39        42  0.0031795392
## 75  Product D Current   Lost 4274.80         9  0.0049907998
## 76  Product D Current    Won 9215.41         3  0.0107589282
## 77  Product B Current    Won 3950.94        39  0.0046126955
## 78  Product B Current    Won 6252.30        27  0.0072995175
## 79  Product C Current    Won 1300.89        42  0.0015187802
## 80  Product B Current    Won 4866.12        22  0.0056811619
## 81  Product B Current    Won 5430.55        38  0.0063401300
## 82  Product B Current    Won 2907.38        43  0.0033943463
## 83  Product B     New    Won 5665.33        68  0.0066142341
## 84  Product B     New   Lost 3219.33         3  0.0037585458
## 85  Product G Current   Lost 9062.59        24  0.0105805119
## 86  Product B     New   Lost 4735.66         1  0.0055288507
## 87  Product B     New   Lost 3690.75        10  0.0043089254
## 88  Product C Current    Won 3579.63         3  0.0041791936
## 89  Product B Current    Won 5276.01        82  0.0061597056
## 90  Product B Current    Won 3231.52        65  0.0037727775
## 91  Product D     New   Lost 5744.26        17  0.0067063843
## 92  Product A Current    Won 5121.83         2  0.0059797015
## 93  Product A Current    Won 4509.96        92  0.0052653475
## 94  Product B Current    Won 4216.81        35  0.0049230969
## 95  Product D Current   Lost 4736.38        73  0.0055296913
## 96  Product J     New   Lost 4269.87        18  0.0049850441
## 97  Product A     New   Lost 1998.75        39  0.0023335270
## 98  Product E Current   Lost 4823.99        35  0.0056319754
## 99  Product F Current    Won 1420.71        33  0.0016586692
## 100 Product A Current    Won 5758.67        23  0.0067232079
## 101 Product H Current    Won 6201.23        48  0.0072398937
## 102 Product H Current    Won 5116.34        63  0.0059732920
## 103 Product D     New   Lost 1196.53        63  0.0013969406
## 104 Product E Current    Won 2910.08        14  0.0033974985
## 105 Product D Current    Won 4110.98        39  0.0047995411
## 106 Product H Current    Won  416.55        47  0.0004863193
## 107 Product H Current    Won 4547.97        13  0.0053097239
## 108 Product H Current    Won 9520.58        99  0.0111152121
## 109 Product H Current    Won 3608.69        56  0.0042131209
## 110 Product H Current    Won 5257.16        22  0.0061376984
## 111 Product B Current    Won  696.88        44  0.0008136026
## 112 Product H     New   Lost  787.57        24  0.0009194826
## 113 Product D Current    Won 3727.65        12  0.0043520059
## 114 Product B Current    Won 3880.07         4  0.0045299552
## 115 Product A Current    Won 3371.70        18  0.0039364367
## 116 Product F Current   Lost 1664.40        58  0.0019431756
## 117 Product B     New   Lost 5287.36        81  0.0061729567
## 118 Product D Current   Lost 3416.82        12  0.0039891140
## 119 Product D Current   Lost 5592.66        85  0.0065293924
## 120 Product B Current    Won 1597.44        37  0.0018650003
## 121 Product D Current   Lost 8180.81        96  0.0095510398
## 122 Product B Current    Won 4831.73        12  0.0056410118
## 123 Product B Current    Won 7219.99        16  0.0084292890
## 124 Product B Current   Lost 3021.30         1  0.0035273471
## 125 Product C Current    Won  482.01        65  0.0005627434
## 126 Product C Current    Won 4796.13        44  0.0055994490
## 127 Product D     New   Lost 4861.06        16  0.0056752543
## 128 Product B Current    Won 2070.25         7  0.0024170027
## 129 Product D Current   Lost 4843.50        80  0.0056547532
## 130 Product B Current    Won 6509.25        53  0.0075995049
## 131 Product A Current    Won 3425.45        82  0.0039991895
## 132 Product B Current    Won 6872.29        25  0.0080233516
## 133 Product B     New   Lost 4429.76        90  0.0051717145
## 134 Product D Current    Won 5992.86        98  0.0069966231
## 135 Product D Current    Won 3976.47        29  0.0046425016
## 136 Product D Current    Won 4814.25        67  0.0056206040
## 137 Product B Current    Won 2924.29        32  0.0034140886
## 138 Product A Current    Won 5145.01        89  0.0060067640
## 139 Product C Current    Won -539.23        11 -0.0006295473
## 140 Product B Current    Won 4566.61         9  0.0053314860
## 141 Product B Current   Lost 5856.18        45  0.0068370502
## 142 Product B Current    Won 4795.64         8  0.0055988769
## 143 Product A Current    Won 3847.71        17  0.0044921751
## 144 Product A Current    Won 5736.79        72  0.0066976631
## 145 Product B     New    Won 3682.25        89  0.0042990017
## 146 Product A Current    Won 4682.94        63  0.0054673005
## 147 Product D Current   Lost 5560.03        68  0.0064912971
## 148 Product D Current   Lost 7969.67        23  0.0093045353
## 149 Product D Current    Won 3485.48        52  0.0040692741
## 150 Product D Current    Won 3415.63        36  0.0039877247
## 151 Product D Current   Lost 7550.60        17  0.0088152739
## 152 Product D Current    Won 6221.36         2  0.0072633953
## 153 Product A Current   Lost 9027.17        71  0.0105391593
## 154 Product D Current   Lost 5436.98        54  0.0063476370
## 155 Product B Current   Lost 6979.77         8  0.0081488338
## 156 Product D Current    Won 4509.53         5  0.0052648455
## 157 Product B Current    Won 6204.95        58  0.0072442367
## 158 Product D Current    Won 5817.77         5  0.0067922067
## 159 Product A Current    Won 4665.88        20  0.0054473830
## 160 Product B Current    Won 1676.09        29  0.0019568236
## 161 Product D Current   Lost 6690.06        60  0.0078105994
## 162 Product D Current    Won 4837.09        31  0.0056472695
## 163 Product D Current    Won 6755.66        59  0.0078871869
## 164 Product C Current    Won 7202.17        31  0.0084084843
## 165 Product F     New    Won 7576.35        29  0.0088453369
## 166 Product B Current   Lost 4371.48         6  0.0051036730
## 167 Product C     New   Lost 3779.86        11  0.0044129607
## 168 Product B Current    Won 4965.08         9  0.0057966970
## 169 Product A Current    Won 8448.77         8  0.0098638813
## 170 Product D Current   Lost 2101.18         5  0.0024531133
## 171 Product D Current   Lost 8031.36        59  0.0093765579
## 172 Product D Current    Won 1340.15        66  0.0015646160
## 173 Product D Current   Lost 3086.88        55  0.0036039113
## 174 Product A Current   Lost 5835.32        23  0.0068126962
## 175 Product D Current    Won 6377.50        12  0.0074456877
## 176 Product D Current    Won 3537.61        28  0.0041301355
## 177 Product A Current    Won 6448.07        34  0.0075280777
## 178 Product D     New   Lost 7320.05        72  0.0085461084
# sample 5 deals without replacement and with replacement
set.seed(31)
deals %>%
  sample_n(5) # without replacement
##     product  client status  amount num_users
## 1 Product D Current   Lost 3086.88        55
## 2 Product C Current   Lost 3727.66        19
## 3 Product D Current   Lost 4274.80         9
## 4 Product B Current    Won 4965.08         9
## 5 Product A Current    Won 5827.35        50
deals %>%
  sample_n(5, replace = TRUE) # with replacement
##     product  client status  amount num_users
## 1 Product A Current    Won 6010.04        24
## 2 Product B Current   Lost 5701.70        53
## 3 Product D Current    Won 6733.62        27
## 4 Product F Current    Won 6780.85        80
## 5 Product C Current    Won -539.23        11
#.............................contiunous distribution.......................... 

# The sales software used at your company is set to automatically back itself up,
# but no one knows exactly what time the back-ups happen. 
#It is known, however, that back-ups happen exactly every 30 minutes.
#Amir comes back from sales meetings at random times to update the data on the client he just met with. 
#He wants to know how long he'll have to wait for his newly-entered data to get backed up. 
#Use your new knowledge of continuous uniform distribution

min <- 0
max <- 30
prob_less_than_5 <- punif(5,min,max)
prob_less_than_5
## [1] 0.1666667
prob_greater_than_5 <- punif(5, min,max, lower.tail = FALSE)
prob_greater_than_5
## [1] 0.8333333
prob_between_10_and_20 <- punif(20,min,max) - punif(10,min,max)
prob_between_10_and_20
## [1] 0.3333333
#To give Amir a better idea of how long he'll have to wait, 
#you'll simulate Amir waiting 1000 times and create a histogram to show him what he should expect. 
#Recall from the last exercise that his minimum wait time is 0 minutes and his maximum wait time is 30 minutes.

set.seed(334)
time = runif(1000, min = 0, max = 30)
df_time = data.frame(time)
ggplot(df_time,aes(time))+geom_histogram(bins = 30)

#.............................binomial distribution.................................

#Assume that Amir usually works on 3 deals per week, and overall, he wins 30% of deals he works on. 
#Each deal has a binary outcome: it's either lost, or won, so you can model his sales deals with a binomial distribution. 
#In this exercise, you'll help Amir simulate a year's worth of his deals so he can better understand his performance.

set.seed(10)
rbinom(1,1,0.3) # simulation of single deal
## [1] 0
rbinom(1,3,0.3) # simulation 1 week of 3 deals
## [1] 0
rbinom(52,3,0.3) # simulation of 52 weeks of 3 deals
##  [1] 1 1 0 0 0 0 1 1 1 1 0 1 1 1 0 0 1 2 2 1 1 1 1 1 2 0 1 1 1 0 0 2 1 1 2 2 1 1
## [39] 0 0 0 1 0 0 0 1 0 2 1 2 0 1
# Just as in the last exercise, assume that Amir wins 30% of deals.
# He wants to get an idea of how likely he is to close a certain number of deals each week. 
# In this exercise, you'll calculate what the chances are of him closing different numbers of deals using the binomial distribution

dbinom(3,3,0.3) # three trials, 3 suceesses, amir wins 30% of deals from previous hisotory
## [1] 0.027
# Now Amir wants to know how many deals he can expect to close each week if his win rate changes. 
# Luckily, you can use your binomial distribution knowledge to help him calculate the expected value in different situations.
# Recall from the video that the expected value of a binomial distribution can be calculated by .

won_30pct <- 3 * 0.3  #Expected number won with 30% win rate
won_30pct
## [1] 0.9
won_25pct <- 3 * 0.25 #Expected number won with 25% win rate
won_25pct
## [1] 0.75
won_35pct <- 3 * 0.35 # Expected number won with 35% win rate
won_35pct
## [1] 1.05
# .....................normal distribution (gaussian distribution).................

# Since each deal Amir worked on (both won and lost) was different, each was worth a different amount of money. 
# These values are stored in the amount column of amir_deals As part of Amir's performance review, 
# you want to be able to estimate the probability of him selling different amounts, 
# but before you can do this, you'll need to determine what kind of distribution the amount variable follows.

ggplot(deals, aes(amount))+geom_histogram(bins=10)

# Since each deal Amir worked on (both won and lost) was different, 
# each was worth a different amount of money. 
# These values are stored in the amount column of amir_deals 
# and follow a normal distribution with a mean of 5000 dollars 
# and a standard deviation of 2000 dollars. 
# As part of his performance metrics, 
# you want to calculate the probability of Amir closing a deal worth various amounts.

pnorm(7500, mean= 5000, sd = 2000) # closing deal with less then $7500
## [1] 0.8943502
pnorm(1000, mean= 5000, sd = 2000, lower.tail = FALSE) # more then $1000
## [1] 0.9772499
pnorm(7000, mean = 5000, sd=2000) - 
  pnorm(3000, mean= 5000, sd= 2000) # between 3000 & 7000
## [1] 0.6826895
qnorm(0.75, mean = 5000, sd = 2000, lower.tail = FALSE) # amount that 75% of deals will be more than
## [1] 3651.02
# The company's financial analyst is predicting that next quarter, 
# the worth of each sale will increase by 20% and the volatility, 
# or standard deviation, of each sale's worth will increase by 30%.
# To see what Amir's sales might look like next quarter under these new market conditions,
# you'll simulate new sales amounts using the normal distribution
# and store these in the new_sales data frame,
# which has already been created for you.

new_mean = 5000*1.3
new_sd = 2000*1.2
new_sales <- deals %>% mutate(amount2 = rnorm(178,mean = new_mean, sd = new_sd))
new_sales
##       product  client status  amount num_users    amount2
## 1   Product F Current    Won 7389.52        19  4406.8188
## 2   Product C     New    Won 4493.01        43  6255.7736
## 3   Product B     New    Won 5738.09        87  5890.9267
## 4   Product I Current    Won 2591.24        83  2051.0229
## 5   Product E Current    Won 6622.97        17  6312.9294
## 6   Product B     New    Won 5496.27         2  8824.5592
## 7   Product C Current    Won 3043.13        29  6943.8223
## 8   Product N Current    Won 7340.64        13  3188.1354
## 9   Product F Current    Won 6780.85        80  3054.7655
## 10  Product B Current    Won 5237.24        23  7369.0093
## 11  Product B Current    Won 2273.55        26  2278.1918
## 12  Product C     New   Lost 2480.33         6  5721.0944
## 13  Product D     New   Lost 4179.72        15  4936.2488
## 14  Product F     New    Won 4918.79        43  9107.7234
## 15  Product B Current   Lost 7030.54        41  4669.8923
## 16  Product B Current    Won 4599.65        12  4511.2099
## 17  Product D Current    Won 5282.57        58  8502.7374
## 18  Product N Current    Won 8627.20         7  4177.6352
## 19  Product A Current    Won 9913.27        34  6430.8432
## 20  Product I Current    Won 3220.45        45  7058.0604
## 21  Product F Current   Lost 6849.12        19  5777.0992
## 22  Product B Current    Won 6236.52         2  4873.7250
## 23  Product F Current    Won 5226.04        82  8072.5463
## 24  Product A Current    Won 1625.19        23  5538.4699
## 25  Product B     New   Lost 3124.62        31  5697.0642
## 26  Product C     New    Won 3342.35        16  9783.0895
## 27  Product B Current   Lost 5701.70        53 11630.6410
## 28  Product B Current    Won 4656.29        26  7713.9662
## 29  Product B Current    Won 4563.53        73  8387.2217
## 30  Product B Current   Lost 5867.54         1  4334.6913
## 31  Product C     New    Won 6151.14        66  7778.9528
## 32  Product B Current   Lost 7404.46        13  4949.8538
## 33  Product I Current    Won 3711.23        14  7198.3700
## 34  Product E     New   Lost 3706.78         3  3529.7733
## 35  Product A     New   Lost 5656.77        33  5405.1769
## 36  Product B Current    Won 8588.35        52  4507.2256
## 37  Product N Current    Won 2738.93        32  7316.2775
## 38  Product F Current    Won 3357.24        53  9059.3033
## 39  Product B Current    Won 2812.45        37  9418.7020
## 40  Product I Current    Won 6533.16        47  8265.6576
## 41  Product B Current    Won 7229.87        99  5345.0993
## 42  Product A Current   Lost 5597.34        89  7850.5874
## 43  Product A Current    Won 5827.35        50  3508.8327
## 44  Product D Current    Won 3328.79        40  7414.2133
## 45  Product B Current    Won 7085.83        17  3066.9746
## 46  Product B Current    Won 8614.12        17  3983.7308
## 47  Product B Current    Won 8222.62         2  5975.5915
## 48  Product C Current   Lost 1548.61        65  2924.1530
## 49  Product C Current   Lost 3727.66        19  9314.4951
## 50  Product B Current    Won 3488.36        79  2948.4151
## 51  Product B     New   Lost 8325.78        74  5467.0692
## 52  Product B Current   Lost 1378.80        42  3976.0673
## 53  Product I     New    Won 7153.77        48 10154.2072
## 54  Product A Current    Won 1649.91        12  7922.7873
## 55  Product A Current    Won 6010.04        24  5965.6124
## 56  Product I Current    Won 1606.86        24  8210.9463
## 57  Product D Current    Won 6820.84        42  8219.8420
## 58  Product C Current   Lost 5411.26        17  7556.5805
## 59  Product B Current    Won 6558.74        90  6881.1935
## 60  Product E Current   Lost 6407.69         6  8083.4339
## 61  Product B Current    Won 6251.99         2 11829.2472
## 62  Product D Current    Won 6733.62        27  3658.5318
## 63  Product F Current    Won 5137.67         3  6322.5060
## 64  Product B Current    Won 4171.76        94  5500.7488
## 65  Product B Current    Won 1640.06        59  6040.4424
## 66  Product B     New   Lost 2746.79        50  6666.9075
## 67  Product G     New   Lost 4279.60        51  9272.8360
## 68  Product B Current    Won 4571.26        19  7927.8976
## 69  Product F Current   Lost 4430.53         7  3092.8517
## 70  Product J     New   Lost 3182.09         2  2643.9746
## 71  Product I Current    Won 2588.60        51  8643.0222
## 72  Product A Current    Won 5945.63        55  6855.6031
## 73  Product B Current   Lost 4606.44        78  9444.8681
## 74  Product D     New   Lost 2723.39        42  4671.6696
## 75  Product D Current   Lost 4274.80         9  7506.5010
## 76  Product D Current    Won 9215.41         3  4004.1359
## 77  Product B Current    Won 3950.94        39  8207.7775
## 78  Product B Current    Won 6252.30        27  4980.2888
## 79  Product C Current    Won 1300.89        42  7851.6192
## 80  Product B Current    Won 4866.12        22  8086.3680
## 81  Product B Current    Won 5430.55        38  2520.6779
## 82  Product B Current    Won 2907.38        43  8967.6031
## 83  Product B     New    Won 5665.33        68  9207.0887
## 84  Product B     New   Lost 3219.33         3  3427.6290
## 85  Product G Current   Lost 9062.59        24  9209.2837
## 86  Product B     New   Lost 4735.66         1  5386.0771
## 87  Product B     New   Lost 3690.75        10  5742.1755
## 88  Product C Current    Won 3579.63         3  8718.3036
## 89  Product B Current    Won 5276.01        82  6685.1473
## 90  Product B Current    Won 3231.52        65  8995.8167
## 91  Product D     New   Lost 5744.26        17  8280.5269
## 92  Product A Current    Won 5121.83         2  9513.3077
## 93  Product A Current    Won 4509.96        92  8782.2055
## 94  Product B Current    Won 4216.81        35  5344.7225
## 95  Product D Current   Lost 4736.38        73  6986.9163
## 96  Product J     New   Lost 4269.87        18  6423.8246
## 97  Product A     New   Lost 1998.75        39  3630.6073
## 98  Product E Current   Lost 4823.99        35  7996.8350
## 99  Product F Current    Won 1420.71        33  4304.4692
## 100 Product A Current    Won 5758.67        23  7097.0192
## 101 Product H Current    Won 6201.23        48  3949.7053
## 102 Product H Current    Won 5116.34        63  5626.4426
## 103 Product D     New   Lost 1196.53        63  3603.2124
## 104 Product E Current    Won 2910.08        14  9930.1107
## 105 Product D Current    Won 4110.98        39  8020.2461
## 106 Product H Current    Won  416.55        47  1707.6425
## 107 Product H Current    Won 4547.97        13  4863.6028
## 108 Product H Current    Won 9520.58        99  5395.8668
## 109 Product H Current    Won 3608.69        56  4140.6339
## 110 Product H Current    Won 5257.16        22  7688.7961
## 111 Product B Current    Won  696.88        44  8241.9620
## 112 Product H     New   Lost  787.57        24  8101.5170
## 113 Product D Current    Won 3727.65        12  8791.4874
## 114 Product B Current    Won 3880.07         4  2479.2028
## 115 Product A Current    Won 3371.70        18  3607.5551
## 116 Product F Current   Lost 1664.40        58  1788.1940
## 117 Product B     New   Lost 5287.36        81 10029.8055
## 118 Product D Current   Lost 3416.82        12  7393.9336
## 119 Product D Current   Lost 5592.66        85  9058.1104
## 120 Product B Current    Won 1597.44        37  7773.5597
## 121 Product D Current   Lost 8180.81        96  6744.7603
## 122 Product B Current    Won 4831.73        12  9710.6779
## 123 Product B Current    Won 7219.99        16  6709.3634
## 124 Product B Current   Lost 3021.30         1  5561.3499
## 125 Product C Current    Won  482.01        65  5900.3180
## 126 Product C Current    Won 4796.13        44  9272.2514
## 127 Product D     New   Lost 4861.06        16  4424.6546
## 128 Product B Current    Won 2070.25         7  4419.9720
## 129 Product D Current   Lost 4843.50        80   929.5591
## 130 Product B Current    Won 6509.25        53  7961.1924
## 131 Product A Current    Won 3425.45        82  9260.0145
## 132 Product B Current    Won 6872.29        25  3620.9656
## 133 Product B     New   Lost 4429.76        90  2707.9982
## 134 Product D Current    Won 5992.86        98  8067.5989
## 135 Product D Current    Won 3976.47        29  5181.4196
## 136 Product D Current    Won 4814.25        67  7750.5309
## 137 Product B Current    Won 2924.29        32  4821.4326
## 138 Product A Current    Won 5145.01        89  5446.6176
## 139 Product C Current    Won -539.23        11  4874.4337
## 140 Product B Current    Won 4566.61         9  8801.9389
## 141 Product B Current   Lost 5856.18        45  2976.3840
## 142 Product B Current    Won 4795.64         8  6941.0333
## 143 Product A Current    Won 3847.71        17  3055.6468
## 144 Product A Current    Won 5736.79        72  3770.2402
## 145 Product B     New    Won 3682.25        89  5504.8512
## 146 Product A Current    Won 4682.94        63  6845.4423
## 147 Product D Current   Lost 5560.03        68  9048.8584
## 148 Product D Current   Lost 7969.67        23  5130.0946
## 149 Product D Current    Won 3485.48        52  9565.2353
## 150 Product D Current    Won 3415.63        36  7047.8944
## 151 Product D Current   Lost 7550.60        17  5758.8486
## 152 Product D Current    Won 6221.36         2  8803.5899
## 153 Product A Current   Lost 9027.17        71  7817.1737
## 154 Product D Current   Lost 5436.98        54  7521.2314
## 155 Product B Current   Lost 6979.77         8  8044.4001
## 156 Product D Current    Won 4509.53         5  3235.2653
## 157 Product B Current    Won 6204.95        58  6023.5853
## 158 Product D Current    Won 5817.77         5  7986.3264
## 159 Product A Current    Won 4665.88        20 11463.7031
## 160 Product B Current    Won 1676.09        29  5767.3166
## 161 Product D Current   Lost 6690.06        60  7174.9895
## 162 Product D Current    Won 4837.09        31  8159.1616
## 163 Product D Current    Won 6755.66        59  6611.2675
## 164 Product C Current    Won 7202.17        31  6771.2705
## 165 Product F     New    Won 7576.35        29  8888.7965
## 166 Product B Current   Lost 4371.48         6  4865.2367
## 167 Product C     New   Lost 3779.86        11  3435.0626
## 168 Product B Current    Won 4965.08         9  2975.1254
## 169 Product A Current    Won 8448.77         8  5747.6622
## 170 Product D Current   Lost 2101.18         5  2411.2172
## 171 Product D Current   Lost 8031.36        59  3258.7648
## 172 Product D Current    Won 1340.15        66  3854.9752
## 173 Product D Current   Lost 3086.88        55  3861.0968
## 174 Product A Current   Lost 5835.32        23  9417.2331
## 175 Product D Current    Won 6377.50        12  7294.1036
## 176 Product D Current    Won 3537.61        28  9836.6603
## 177 Product A Current    Won 6448.07        34  8592.9128
## 178 Product D     New   Lost 7320.05        72  3906.0391
ggplot(new_sales, aes(amount2))+geom_histogram(bins = 10)

# The key metric that the company uses to evaluate salespeople is the percent of sales they make over $1000 
# since the time put into each sale is usually worth a bit more than that,
# so the higher this metric, the better the salesperson is performing.
# Recall that Amir's current sales amounts have a mean of $5000 and a standard deviation of $2000,
# and Amir's predicted amounts in next quarter's market have a mean of $6000 and a standard deviation of $2600.
# Based only on the metric of percent of sales over $1000, 
# does Amir perform better in the current market or the predicted market?
# answer: amir performs about equally in both market.  

pnorm(1000, mean = 6000, sd = 2600, lower.tail = FALSE) # for next quarter's market
## [1] 0.9727648
pnorm(1000, mean = 5000, sd = 2000, lower.tail = FALSE) # on present
## [1] 0.9772499
# .......................... central limit theorum..............................

# The central limit theorem states that 
# a sampling distribution of a sample statistic approaches the normal distribution as you take more samples,
# no matter the original distribution being sampled from.
# In this exercise, you'll focus on the sample mean
# and see the central limit theorem in action 
# while examining the num_users column of amir_deals more closely, 
# which contains the number of people who intend to use the product Amir is selling.

ggplot(deals, aes(num_users)) + geom_histogram(bins = 10)

set.seed(104)
sample(deals$num_users, size = 20, replace = TRUE) %>% mean()
## [1] 30.35
sample_means <- replicate(100,sample(deals$num_users, size = 20, replace = TRUE) %>% mean())
samples <- data.frame(mean = sample_means)
samples
##      mean
## 1   39.60
## 2   42.35
## 3   27.50
## 4   46.25
## 5   37.35
## 6   42.95
## 7   45.85
## 8   45.25
## 9   37.10
## 10  45.00
## 11  36.00
## 12  39.05
## 13  33.20
## 14  31.55
## 15  46.65
## 16  52.80
## 17  42.10
## 18  40.55
## 19  41.75
## 20  37.60
## 21  37.05
## 22  38.20
## 23  34.35
## 24  61.45
## 25  45.95
## 26  24.50
## 27  45.80
## 28  39.55
## 29  43.40
## 30  32.35
## 31  39.75
## 32  37.30
## 33  34.75
## 34  32.65
## 35  44.00
## 36  35.00
## 37  22.00
## 38  30.95
## 39  37.65
## 40  41.25
## 41  40.65
## 42  50.10
## 43  38.30
## 44  41.05
## 45  46.20
## 46  47.90
## 47  31.80
## 48  42.80
## 49  41.60
## 50  43.90
## 51  33.90
## 52  43.25
## 53  36.90
## 54  42.00
## 55  31.20
## 56  33.25
## 57  38.05
## 58  35.05
## 59  30.75
## 60  42.70
## 61  35.85
## 62  30.90
## 63  43.05
## 64  36.50
## 65  26.90
## 66  37.45
## 67  23.70
## 68  47.65
## 69  30.30
## 70  31.25
## 71  37.65
## 72  37.30
## 73  44.00
## 74  41.45
## 75  41.40
## 76  26.30
## 77  31.90
## 78  30.70
## 79  49.35
## 80  35.65
## 81  42.80
## 82  36.05
## 83  42.20
## 84  35.65
## 85  43.75
## 86  52.45
## 87  34.60
## 88  38.80
## 89  38.35
## 90  30.65
## 91  32.90
## 92  27.55
## 93  26.30
## 94  36.20
## 95  39.80
## 96  45.55
## 97  46.20
## 98  42.95
## 99  46.20
## 100 45.35
ggplot(samples, aes(mean)) +
  geom_histogram(bins = 10)

# ...........................poission distribution.......................

# Your company uses sales software to keep track of new sales leads.
# It organizes them into a queue so that anyone can follow up on one when they have a bit of free time.
# Since the number of lead responses is a countable outcome over a period of time,
# this scenario corresponds to a Poisson distribution. 
# On average, Amir responds to 4 leads each day.
# In this exercise, you'll calculate probabilities of Amir responding to different numbers of leads.
# What's the probability that Amir responds to 5 leads in a day,
# given that he responds to an average of 4?
dpois( 5, lambda = 4)
## [1] 0.1562935
# Amir's coworker responds to an average of 5.5 leads per day.
# What is the probability that she answers 5 leads in a day?
dpois( 5 , lambda = 5.5 )
## [1] 0.1714007
# What's the probability that Amir responds to 2 or fewer leads in a day?
ppois( 2, lambda = 4)
## [1] 0.2381033
# What's the probability that Amir responds to more than 10 leads in a day?
ppois(10, lambda = 4)
## [1] 0.9971602
# ..........................expotencial distribution..........................

# To further evaluate Amir's performance, you want to know how much time it takes him to respond to a lead after he opens it.
# On average, it takes 2.5 hours for him to respond.
# In this exercise, you'll calculate probabilities of different amounts of time passing between Amir receiving a lead and sending a response.
# Probability response takes < 1 hour
pexp(1, rate = 1/2.5)
## [1] 0.32968
# Probability response takes > 4 hour
pexp(4, rate = 1/2.5, lower.tail = FALSE)
## [1] 0.2018965
# Probability response takes 3-4 hours
pexp(4, rate=1/2.5) - pexp(3, rate=1/2.5)
## [1] 0.09929769
#..............................correlation...............................

# relationship between variables
happiness = readRDS("/Users/abdullahibnmasud/statistics/data/world_happiness_sugar.rds")
happiness
##                      country social_support freedom corruption generosity
## 1                    Finland              2       5          4         47
## 2                    Denmark              4       6          3         22
## 3                     Norway              3       3          8         11
## 4                    Iceland              1       7         45          3
## 5                Netherlands             15      19         12          7
## 6                Switzerland             13      11          7         16
## 7                     Sweden             25      10          6         17
## 8                New Zealand              5       8          5          8
## 9                     Canada             20       9         11         14
## 10                   Austria             31      26         19         25
## 11                 Australia              7      17         13          6
## 12                Costa Rica             42      16         58         75
## 13                    Israel             38      93         74         24
## 14                Luxembourg             27      28          9         30
## 15            United Kingdom              9      63         15          4
## 16                   Ireland              6      33         10          9
## 17                   Germany             39      44         17         19
## 18                   Belgium             22      53         20         44
## 19             United States             37      62         42         12
## 20            Czech Republic             24      58        121        117
## 21      United Arab Emirates             72       4         NA         15
## 22                     Malta             16      12         32          5
## 23                    Mexico             67      71         87        120
## 24                    France             32      69         21         68
## 25                     Chile             58      98         99         45
## 26                 Guatemala             78      25         82         78
## 27              Saudi Arabia             62      68         NA         82
## 28                     Spain             26      95         78         50
## 29                    Panama             41      32        104         88
## 30                    Brazil             43      84         71        108
## 31                   Uruguay             35      30         33         80
## 32               El Salvador             83      74         85        134
## 33                     Italy             23     132        128         48
## 34       Trinidad and Tobago             29      51        141         41
## 35                    Poland             44      52        108         77
## 36                Uzbekistan             11       1         18         29
## 37                 Lithuania             17     122        113        124
## 38                  Colombia             52      56        124        111
## 39                  Slovenia             14      13         97         54
## 40                 Nicaragua             66      70         43         71
## 41                 Argentina             46      54        109        123
## 42                   Romania             86      57        146        102
## 43                    Cyprus             90      81        115         39
## 44                   Ecuador             71      42         68         95
## 45                    Kuwait             69      47         NA         42
## 46                  Thailand             53      18        131         10
## 47                    Latvia             34     126         92        105
## 48               South Korea             91     144        100         40
## 49                   Estonia             12      45         30         83
## 50                   Jamaica             28      49        130        119
## 51                 Mauritius             54      40         96         37
## 52                     Japan             50      64         39         92
## 53                  Honduras             84      39         79         51
## 54                Kazakhstan             19      80         57         57
## 55                   Bolivia             93      35         91        104
## 56                   Hungary             51     138        140        100
## 57                  Paraguay             30      34         76         67
## 58                      Peru             77      61        132        126
## 59                  Portugal             47      37        135        122
## 60                  Pakistan            130     114         55         58
## 61                    Russia             40     107        127        101
## 62               Philippines             75      15         49        115
## 63                    Serbia             57     124        118         84
## 64                   Moldova             65     128        148         86
## 65                Montenegro             60     139         77         76
## 66                Tajikistan            113      86         35         72
## 67                   Croatia             79     118        139         81
## 68        Dominican Republic             55      43         52         93
## 69                    Turkey             61     140         50         98
## 70                  Malaysia             97      36        137         27
## 71                   Belarus             33     131         37        103
## 72                    Greece            102     150        123        152
## 73                  Mongolia             10     112        119         38
## 74                   Nigeria            111      75        114         59
## 75              Turkmenistan              8      83         NA         33
## 76                   Algeria            101     149         46        128
## 77                   Morocco            139      76         84        154
## 78                Azerbaijan            104     101         22        146
## 79                   Lebanon             89     136        133         63
## 80                 Indonesia             94      48        129          2
## 81                     China            108      31         NA        133
## 82                   Vietnam             64      23         86         97
## 83                  Cameroon            129      90        120         91
## 84                  Bulgaria             18     115        147        112
## 85                     Ghana            132      91        117         52
## 86                     Nepal             87      67         65         46
## 87                    Jordan             88      88         NA        118
## 88                     Benin            153     103         75        116
## 89                     Gabon             95     119        103        143
## 90              South Africa             63      85        102         89
## 91                   Albania            133      87        134         60
## 92                 Venezuela             49     145        110        139
## 93                  Cambodia            109       2         94         61
## 94                   Senegal            106     121         88        130
## 95                   Namibia             70      82         98        142
## 96                     Niger            140     111         51        135
## 97              Burkina Faso            116     127         47        125
## 98                   Armenia            117     123         93        129
## 99                      Iran            134     117         44         28
## 100                   Guinea            136     109         70         94
## 101                  Georgia            147     104         28        153
## 102                   Gambia            125      89         26         64
## 103                    Kenya            123      72        105         26
## 104               Mauritania             99     151         67        148
## 105               Mozambique            122      46         40        121
## 106                  Tunisia            121     143        101        144
## 107               Bangladesh            126      27         36        107
## 108                     Iraq            124     130         66         73
## 109                     Mali            112     110        107        138
## 110             Sierra Leone            135     116        112         79
## 111                Sri Lanka             80      55        111         35
## 112                  Myanmar             96      29         24          1
## 113                     Chad            141     142         80        106
## 114                  Ukraine             56     141        143         66
## 115                 Ethiopia            119     106         53         99
## 116                Swaziland            103     113         41        145
## 117                   Uganda            114      99         95         74
## 118                    Egypt            118     129         89        132
## 119                   Zambia            115      73         69         53
## 120                     Togo            149     120         72        131
## 121                    India            142      41         73         65
## 122                  Liberia            127      94        126        110
## 123               Madagascar            128     146        116        136
## 124                  Lesotho             98      97         59        151
## 125                 Zimbabwe            110      96         63        141
## 126                    Haiti            146     152         48         20
## 127                 Botswana            105      60         54        150
## 128                   Malawi            150      65         64        109
## 129                    Yemen            100     147         83        155
## 130                   Rwanda            144      21          2         90
## 131                 Tanzania            131      78         34         49
## 132              Afghanistan            151     155        136        137
## 133 Central African Republic            155     133        122        113
##     gdp_per_cap life_exp happiness_score grams_sugar_per_day
## 1         42400     81.8             155                86.8
## 2         48300     81.0             154               152.0
## 3         66300     82.6             153               120.0
## 4         47900     83.0             152               132.0
## 5         50500     81.8             151               122.0
## 6         59000     84.3             150               166.0
## 7         47200     82.8             149               115.0
## 8         36500     81.9             148               162.0
## 9         44200     82.2             147               132.0
## 10        46900     82.0             146               124.0
## 11        45500     82.7             145               126.0
## 12        15800     79.8             144               144.0
## 13        34100     83.3             143                76.2
## 14        94300     82.0             142               141.0
## 15        40400     81.1             141               113.0
## 16        72400     82.3             140               126.0
## 17        46200     80.9             139               133.0
## 18        43500     81.7             138               144.0
## 19        56700     78.6             137               175.0
## 20        34100     79.5             136               118.0
## 21        65700     73.6             135               101.0
## 22        39300     81.3             134               168.0
## 23        18000     75.6             133               133.0
## 24        40000     83.1             132               107.0
## 25        23000     80.0             130               131.0
## 26         7610     73.1             129               132.0
## 27        48100     77.1             128                86.8
## 28        35700     83.3             126                85.6
## 29        23300     79.7             125                95.3
## 30        14300     75.9             124               117.0
## 31        20900     77.3             123               111.0
## 32         7510     74.5             121               113.0
## 33        35800     83.5             120                88.0
## 34        28500     74.5             117               162.0
## 35        29900     78.4             116               122.0
## 36         6490     70.7             115                27.6
## 37        32400     75.4             114               130.0
## 38        13600     80.5             113               160.0
## 39        33700     81.4             112                75.1
## 40         4620     79.2             111               116.0
## 41        17500     77.0             109               132.0
## 42        25500     75.4             108                70.1
## 43        34500     82.0             107               101.0
## 44        10200     77.2             106                63.3
## 45        64100     83.3             105               106.0
## 46        17400     78.6             104               109.0
## 47        27300     75.4             103               117.0
## 48        37300     83.0             102               105.0
## 49        32000     78.5             101                86.5
## 50         8310     74.9             100               135.0
## 51        21800     75.0              99                96.0
## 52        39700     84.5              98                74.2
## 53         4630     74.3              97               131.0
## 54        25400     72.8              96                78.4
## 55         7150     73.3              95                75.2
## 56        29600     77.2              94                95.0
## 57        12000     76.4              93                69.6
## 58        13000     80.8              91                62.9
## 59        29300     81.8              90                78.0
## 60         5000     67.2              89                76.0
## 61        25700     72.5              88               134.0
## 62         8260     70.0              87                64.6
## 63        16700     76.0              86                77.1
## 64         6720     73.1              85                64.6
## 65        17800     76.7              83               131.0
## 66         3150     70.8              82                51.9
## 67        24500     78.8              81               118.0
## 68        16400     73.5              79                95.3
## 69        25000     79.5              77                87.3
## 70        29100     75.0              76               121.0
## 71        18000     74.5              75               107.0
## 72        25700     81.3              74                80.3
## 73        12800     69.3              73                40.3
## 74         5300     65.2              71                29.4
## 75        17600     70.3              69                24.6
## 76        14000     78.1              68                78.2
## 77         7770     74.4              67               109.0
## 78        16200     71.1              66                50.5
## 79        11700     78.1              65               123.0
## 80        12100     71.9              64                46.5
## 81        17100     77.5              63                18.8
## 82         6970     74.7              62                27.4
## 83         3400     63.8              60                27.2
## 84        20200     75.1              59                78.7
## 85         4430     66.1              58                40.5
## 86         2880     71.5              56                13.0
## 87         8350     79.6              55               128.0
## 88         2230     65.3              54                21.4
## 89        16200     69.0              52                42.6
## 90        12000     66.9              50               101.0
## 91        12700     78.5              49               131.0
## 92         9720     75.1              48               106.0
## 93         4080     70.4              47                51.5
## 94         3460     68.6              45                50.0
## 95         9700     67.2              43                55.4
## 96          954     63.2              42                14.5
## 97         1810     62.5              41                20.3
## 98         9730     75.9              40                97.4
## 99        16100     77.8              39                79.8
## 100        2420     61.6              38                27.2
## 101       10700     73.2              37                93.1
## 102        1570     66.4              36                72.2
## 103        3160     66.7              35                43.7
## 104        3890     71.0              34                99.2
## 105        1170     59.9              33                30.7
## 106       11100     78.7              32                95.5
## 107        4140     73.7              31                22.1
## 108       15700     77.1              30                52.8
## 109        2100     62.9              28                28.1
## 110        1460     61.3              27                16.5
## 111       12100     77.8              26                73.4
## 112        6270     69.1              25                46.2
## 113        1740     60.6              24                29.1
## 114        8480     70.8              23               126.0
## 115        1900     69.1              22                18.1
## 116        9560     61.8              21                95.3
## 117        1870     66.6              20                32.4
## 118       11400     71.0              19                78.0
## 119        3700     64.0              18                28.1
## 120        1610     65.0              17                33.3
## 121        7230     69.5              16                64.2
## 122        1140     65.2              15                15.5
## 123        1490     64.2              13                20.3
## 124        2920     56.1              12                45.5
## 125        2390     62.0              10                80.6
## 126        1640     65.7               9                45.8
## 127       16800     69.8               8                85.0
## 128        1180     64.2               6                22.6
## 129        2340     68.1               5                77.9
## 130        2110     69.1               4                14.1
## 131        2980     67.7               3                28.0
## 132        1760     64.1               2                24.5
## 133         794     52.9               1                22.4
ggplot(happiness, aes(life_exp,happiness_score)) + geom_point() +
  geom_smooth(method = "lm", se = FALSE) # scatter ploting and linear line drawn to show coverance
## `geom_smooth()` using formula = 'y ~ x'

cor(happiness$life_exp, happiness$happiness_score) # correlation
## [1] 0.7737615
# correlation only measures linear relationship
ggplot(happiness, aes(gdp_per_cap, life_exp)) + geom_point() # scattering gdp_per_cap and life_exp

cor(happiness$gdp_per_cap, happiness$life_exp) # not correct
## [1] 0.7235027
happiness <- happiness %>%
  mutate(log_gdp_per_cap = log(gdp_per_cap)) # transformation scattering 
ggplot(happiness, aes(log_gdp_per_cap, life_exp)) + geom_point() # scattering log_gdp_per_cap and life_exp

cor(happiness$log_gdp_per_cap, happiness$life_exp) # correct
## [1] 0.8562314
# does suger improve happniess?
ggplot(happiness, aes(grams_sugar_per_day, happiness_score)) +
  geom_point() # Scatterplot of grams_sugar_per_day and happiness_score

cor(happiness$grams_sugar_per_day, happiness$happiness_score)
## [1] 0.69391
# more suger consumption doesnot lead higher happniess score but associated with higher happiness score.
# Nice interpretation of correlation! 
# If correlation always implied that one thing causes another, 
# people may do some nonsensical things, like eat more sugar to be happier.

#....................type of studies...............................
# longitudinal studies: same subjects or participants repeatedly at multiple time points. They aim to track changes, trends, or developments within individuals or groups over time
# cross-sectional studies are conducted at a single point in time, capturing a snapshot of a population or group. Researchers collect data from different individuals or groups at a specific moment to examine relationships between variables.