question 1

data("cars")
median(cars$speed)
## [1] 15

question 2

# install.packages("jsonlite", dependencies = TRUE)
library(jsonlite)
url <- "https://min-api.cryptocompare.com/data/v2/histoday?fsym=BTC&tsym=USD&limit=100&tryConversion=true"
btc_data <- fromJSON(url) 
btc_df <- btc_data$Data$Data
max.close <- max(btc_df$close, na.rm = TRUE)
max.close
## [1] 124723
btc_df
##           time     high      low     open volumefrom   volumeto    close
## 1   1752019200 112077.2 108341.9 108955.1   19761.34 2177951433 111291.7
## 2   1752105600 116848.3 110555.5 111291.7   31905.27 3616719328 116023.3
## 3   1752192000 118890.3 115236.7 116023.3   26050.27 3060911543 117573.9
## 4   1752278400 118240.0 116954.1 117573.9    7395.67  869899617 117468.2
## 5   1752364800 119503.6 117264.6 117468.2    9553.52 1133368268 119127.7
## 6   1752451200 123220.3 118951.6 119127.7   31361.70 3784774921 119869.0
## 7   1752537600 119958.9 115709.6 119869.0   50123.19 5881848281 117777.9
## 8   1752624000 120122.2 117043.9 117777.9   27837.68 3310853210 118700.4
## 9   1752710400 121020.9 117486.0 118700.4   25218.52 3000532789 119282.7
## 10  1752796800 120911.2 116902.9 119282.7   26111.51 3096339405 118025.9
## 11  1752883200 118572.5 117339.4 118025.9    6706.12  791893577 117913.3
## 12  1752969600 118903.9 116533.2 117913.3    9166.19 1081488186 117328.6
## 13  1753056000 119712.2 116581.7 117328.6   18543.72 2187836281 117441.8
## 14  1753142400 120298.6 116186.2 117441.8   21981.53 2607540535 120023.5
## 15  1753228800 120167.5 117361.9 120023.5   17163.70 2029832616 118808.8
## 16  1753315200 119555.9 117214.4 118808.8   17930.71 2128340081 118397.3
## 17  1753401600 118523.3 114756.4 118397.3   47767.94 5546818212 117644.1
## 18  1753488000 118355.1 117143.7 117644.1   10959.83 1291296799 117971.3
## 19  1753574400 119808.3 117872.1 117971.3   10341.68 1228024808 119468.7
## 20  1753660800 119835.4 117403.3 119468.7   18092.60 2142740462 118059.9
## 21  1753747200 119286.0 116933.7 118059.9   17580.06 2073820445 117942.1
## 22  1753833600 118800.1 115769.5 117942.1   18335.33 2153325010 117853.3
## 23  1753920000 118927.1 115493.8 117853.3   16934.89 1991848800 115765.4
## 24  1754006400 116060.7 112682.8 115765.4   33588.86 3851719229 113263.5
## 25  1754092800 114034.0 112006.4 113263.5   12452.57 1407609649 112546.3
## 26  1754179200 114803.5 111925.9 112546.3    8326.85  947196437 114238.8
## 27  1754265600 115744.6 114140.4 114238.8   14470.62 1662390457 115065.9
## 28  1754352000 115111.4 112629.5 115065.9   16922.91 1925704360 114127.6
## 29  1754438400 115748.2 113364.7 114127.6   14480.66 1659630183 115038.4
## 30  1754524800 117690.9 114288.6 115038.4   16359.62 1904030479 117522.3
## 31  1754611200 117698.4 115895.4 117522.3   14582.39 1702088353 116692.9
## 32  1754697600 117940.3 116360.1 116692.9    7930.51  926558904 116501.3
## 33  1754784000 119320.8 116496.4 116501.3   10554.12 1248946431 119311.7
## 34  1754870400 122309.7 118106.9 119311.7   25659.91 3086540255 118714.6
## 35  1754956800 120326.0 118214.3 118714.6   18213.63 2172908090 120128.2
## 36  1755043200 123735.4 118948.2 120128.2   27352.96 3321096940 123374.6
## 37  1755129600 124532.7 117241.1 123374.6   35491.88 4249355542 118391.6
## 38  1755216000 119335.9 116865.9 118391.6   19724.55 2327158152 117440.1
## 39  1755302400 118000.6 117242.7 117440.1    6405.98  753827490 117469.5
## 40  1755388800 118639.8 117268.7 117469.5    6706.31  791200442 117488.0
## 41  1755475200 117627.7 114716.5 117488.0   22732.79 2634618668 116292.1
## 42  1755561600 116789.9 112738.0 116292.1   26286.85 3006851750 112870.7
## 43  1755648000 114642.3 112387.1 112870.7   22978.24 2610717314 114291.4
## 44  1755734400 114816.8 111993.8 114291.4   19947.31 2256735255 112491.8
## 45  1755820800 117428.1 111675.1 112491.8   28375.72 3261042644 116909.8
## 46  1755907200 117003.3 114533.5 116909.8    8271.23  955311764 115391.8
## 47  1755993600 115626.3 110860.7 115391.8   17025.82 1932434686 113486.7
## 48  1756080000 113648.6 109292.2 113486.7   33305.33 3708998747 110150.1
## 49  1756166400 112407.5 108701.3 110150.1   27215.82 2999152225 111805.9
## 50  1756252800 112669.1 110381.6 111805.9   21850.84 2440706984 111277.0
## 51  1756339200 113487.6 110887.4 111277.0   16865.13 1898278606 112582.2
## 52  1756425600 112646.7 107512.4 112582.2   29122.98 3185827655 108393.1
## 53  1756512000 108937.2 107381.3 108393.1    9682.99 1049906248 108833.5
## 54  1756598400 109510.2 108095.9 108833.5    9099.54  990157825 108273.7
## 55  1756684800 109913.3 107271.4 108273.7   21783.85 2365084072 109256.3
## 56  1756771200 111796.2 108420.8 109256.3   27112.92 2993969176 111245.9
## 57  1756857600 112601.7 110556.5 111245.9   19087.45 2132239040 111751.5
## 58  1756944000 112225.9 109345.2 111751.5   19577.20 2162838408 110732.3
## 59  1757030400 113400.4 110217.3 110732.3   28328.28 3163045481 110677.0
## 60  1757116800 111320.3 110019.9 110677.0    6640.60  734143439 110230.2
## 61  1757203200 111607.9 110215.7 110230.2    6492.41  720954382 111143.2
## 62  1757289600 112937.8 110628.2 111143.2   16345.84 1829788896 112089.1
## 63  1757376000 113293.1 110777.6 112089.1   18844.58 2108163610 111549.1
## 64  1757462400 114341.9 110940.6 111549.1   23523.08 2661952162 113988.2
## 65  1757548800 115543.7 113457.5 113988.2   18345.68 2096575533 115537.8
## 66  1757635200 116816.5 114785.3 115537.8   20842.83 2412089976 116116.3
## 67  1757721600 116361.1 115207.4 116116.3    7083.57  820638135 115971.9
## 68  1757808000 116227.4 115203.8 115971.9    7290.45  843593558 115351.9
## 69  1757894400 116808.1 114427.7 115351.9   18559.61 2137738674 115401.1
## 70  1757980800 117006.6 114765.7 115401.1   16305.16 1890323068 116838.3
## 71  1758067200 117331.1 114742.8 116838.3   23702.53 2750924038 116488.2
## 72  1758153600 117981.1 116134.8 116488.2   19195.15 2253362090 117126.5
## 73  1758240000 117511.0 115150.9 117126.5   16753.48 1947524484 115702.0
## 74  1758326400 116202.3 115488.4 115702.0    5995.45  694697640 115756.1
## 75  1758412800 115901.0 115262.9 115756.1    5422.99  626782372 115299.8
## 76  1758499200 115439.0 112027.4 115299.8   28253.02 3197786130 112747.9
## 77  1758585600 113372.6 111518.3 112747.9   18488.69 2080069064 112043.7
## 78  1758672000 114013.4 111118.7 112043.7   17597.56 1988342352 113360.7
## 79  1758758400 113559.7 108664.3 113360.7   34940.33 3874673082 109061.1
## 80  1758844800 110387.3 108695.1 109061.1   27943.91 3057709370 109723.9
## 81  1758931200 109822.4 109156.3 109723.9    7105.98  778286679 109727.4
## 82  1759017600 112407.0 109280.1 109727.4   11935.68 1316899924 112226.6
## 83  1759104000 114496.6 111625.4 112226.6   19529.19 2209223704 114407.1
## 84  1759190400 114866.5 112727.8 114407.1   22976.41 2612642607 114078.2
## 85  1759276800 118722.7 114004.9 114078.2   28249.14 3288729605 118666.7
## 86  1759363200 121107.2 118356.6 118666.7   27985.21 3348607183 120630.7
## 87  1759449600 123996.4 119348.6 120630.7   35664.64 4338240509 122318.9
## 88  1759536000 122898.1 121598.2 122318.9    9977.25 1218962840 122455.4
## 89  1759622400 125748.8 122213.4 122455.4   21834.57 2703677561 123535.2
## 90  1759708800 126287.3 123143.5 123535.2   27874.80 3478551799 124723.0
## 91  1759795200 125225.2 120661.8 124723.0   29039.60 3568433458 121414.5
## 92  1759881600 124254.9 121116.3 121414.5   25262.59 3098621662 123358.6
## 93  1759968000 123818.9 119741.9 123358.6   26848.29 3266509707 121713.9
## 94  1760054400 122586.6 105833.8 121713.9   63865.23 7411664103 112998.9
## 95  1760140800 113462.0 109711.0 112998.9   38127.92 4260858252 110779.6
## 96  1760227200 115849.5 109708.3 110779.6   36833.57 4154852091 115084.7
## 97  1760313600 116064.3 113754.7 115084.7   30207.33 3475980079 115279.0
## 98  1760400000 115514.5 109983.3 115279.0   39286.64 4415300610 113097.8
## 99  1760486400 113679.9 110229.7 113097.8   30171.93 3370232022 110819.5
## 100 1760572800 112027.4 107501.5 110819.5   44973.38 4931780713 108212.4
## 101 1760659200 109278.9 103552.1 108212.4   33208.93 3521182277 105670.5
##     conversionType conversionSymbol
## 1           direct                 
## 2           direct                 
## 3           direct                 
## 4           direct                 
## 5           direct                 
## 6           direct                 
## 7           direct                 
## 8           direct                 
## 9           direct                 
## 10          direct                 
## 11          direct                 
## 12          direct                 
## 13          direct                 
## 14          direct                 
## 15          direct                 
## 16          direct                 
## 17          direct                 
## 18          direct                 
## 19          direct                 
## 20          direct                 
## 21          direct                 
## 22          direct                 
## 23          direct                 
## 24          direct                 
## 25          direct                 
## 26          direct                 
## 27          direct                 
## 28          direct                 
## 29          direct                 
## 30          direct                 
## 31          direct                 
## 32          direct                 
## 33          direct                 
## 34          direct                 
## 35          direct                 
## 36          direct                 
## 37          direct                 
## 38          direct                 
## 39          direct                 
## 40          direct                 
## 41          direct                 
## 42          direct                 
## 43          direct                 
## 44          direct                 
## 45          direct                 
## 46          direct                 
## 47          direct                 
## 48          direct                 
## 49          direct                 
## 50          direct                 
## 51          direct                 
## 52          direct                 
## 53          direct                 
## 54          direct                 
## 55          direct                 
## 56          direct                 
## 57          direct                 
## 58          direct                 
## 59          direct                 
## 60          direct                 
## 61          direct                 
## 62          direct                 
## 63          direct                 
## 64          direct                 
## 65          direct                 
## 66          direct                 
## 67          direct                 
## 68          direct                 
## 69          direct                 
## 70          direct                 
## 71          direct                 
## 72          direct                 
## 73          direct                 
## 74          direct                 
## 75          direct                 
## 76          direct                 
## 77          direct                 
## 78          direct                 
## 79          direct                 
## 80          direct                 
## 81          direct                 
## 82          direct                 
## 83          direct                 
## 84          direct                 
## 85          direct                 
## 86          direct                 
## 87          direct                 
## 88          direct                 
## 89          direct                 
## 90          direct                 
## 91          direct                 
## 92          direct                 
## 93          direct                 
## 94          direct                 
## 95          direct                 
## 96          direct                 
## 97          direct                 
## 98          direct                 
## 99          direct                 
## 100         direct                 
## 101         direct

question 3 - research project (What Makes You Happy? Analyzing different countries of Life Satisfaction)

1. Do countries with higher social support report higher life satisfaction?

# install.packages("RKaggle")
library(RKaggle)
# install.packages("tidyr")
library(tidyr)
library(readr)
# install.packages("httr")
library(httr) 
# install.packages("readxl")
library(readxl)
happiness <- read.csv("~/Downloads/archive 2/2019.csv") 
happiness_2 <- read.csv("~/Downloads/archive (1)/World-happiness-report-updated_2024.csv")
colnames(happiness) <- c("Overall Rank", "Country/Region", "Life Satisfaction Score", "GDP per Capita", "Social Support", "Life Expectancy", "Freedom of Choice", "Generosity", "Perception of Corruption")
# install.packages("dplyr")
library(dplyr)
## 
## 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
glimpse(happiness)
## Rows: 156
## Columns: 9
## $ `Overall Rank`             <int> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, …
## $ `Country/Region`           <chr> "Finland", "Denmark", "Norway", "Iceland", …
## $ `Life Satisfaction Score`  <dbl> 7.769, 7.600, 7.554, 7.494, 7.488, 7.480, 7…
## $ `GDP per Capita`           <dbl> 1.340, 1.383, 1.488, 1.380, 1.396, 1.452, 1…
## $ `Social Support`           <dbl> 1.587, 1.573, 1.582, 1.624, 1.522, 1.526, 1…
## $ `Life Expectancy`          <dbl> 0.986, 0.996, 1.028, 1.026, 0.999, 1.052, 1…
## $ `Freedom of Choice`        <dbl> 0.596, 0.592, 0.603, 0.591, 0.557, 0.572, 0…
## $ Generosity                 <dbl> 0.153, 0.252, 0.271, 0.354, 0.322, 0.263, 0…
## $ `Perception of Corruption` <dbl> 0.393, 0.410, 0.341, 0.118, 0.298, 0.343, 0…
summary(happiness)
##   Overall Rank    Country/Region     Life Satisfaction Score GDP per Capita  
##  Min.   :  1.00   Length:156         Min.   :2.853           Min.   :0.0000  
##  1st Qu.: 39.75   Class :character   1st Qu.:4.545           1st Qu.:0.6028  
##  Median : 78.50   Mode  :character   Median :5.380           Median :0.9600  
##  Mean   : 78.50                      Mean   :5.407           Mean   :0.9051  
##  3rd Qu.:117.25                      3rd Qu.:6.184           3rd Qu.:1.2325  
##  Max.   :156.00                      Max.   :7.769           Max.   :1.6840  
##  Social Support  Life Expectancy  Freedom of Choice   Generosity    
##  Min.   :0.000   Min.   :0.0000   Min.   :0.0000    Min.   :0.0000  
##  1st Qu.:1.056   1st Qu.:0.5477   1st Qu.:0.3080    1st Qu.:0.1087  
##  Median :1.272   Median :0.7890   Median :0.4170    Median :0.1775  
##  Mean   :1.209   Mean   :0.7252   Mean   :0.3926    Mean   :0.1848  
##  3rd Qu.:1.452   3rd Qu.:0.8818   3rd Qu.:0.5072    3rd Qu.:0.2482  
##  Max.   :1.624   Max.   :1.1410   Max.   :0.6310    Max.   :0.5660  
##  Perception of Corruption
##  Min.   :0.0000          
##  1st Qu.:0.0470          
##  Median :0.0855          
##  Mean   :0.1106          
##  3rd Qu.:0.1412          
##  Max.   :0.4530
plot(happiness$"Social Support", happiness$`Life Satisfaction Score`,
     main="Social Support vs. Life Satisfaction",
     xlab="Social Support",
     ylab="Life Satisfaction (Score)",
     pch=16)

## Based on the plot chart, as social support increases, life satisfaction increases.

2. How does GDP per Capita affect overall happiness?

library(ggplot2)
ggplot(happiness, aes(x = `GDP per Capita`, y = `Life Satisfaction Score`)) +
  geom_point() +
  ggtitle("Happiness Score vs. GDP per Capita") +
  xlab("GDP per Capita") +
  ylab("Happiness Score")

ggplot(happiness, aes(x = `GDP per Capita`, y = `Life Satisfaction Score`)) +
  geom_point() +
  geom_smooth(method = "lm", se = FALSE, color = "red") + 
  ggtitle("Happiness Score vs. GDP per Capita") +
  xlab("GDP per Capita") +
  ylab("Happiness Score")
## `geom_smooth()` using formula = 'y ~ x'

## Based on this, as GDP per capita increases, so does the Happiness score.

3. Which regions/countries have declined in happiness over the last decade? Which have improved?

library(dplyr)
colnames(happiness_2) <- c("Country", "Year", "Life_Satisfaction_Score", "GDP_per _Capita", "Social_Support", "Life_Expectancy_at_Birth", "Freedom_to_make_Life_Choices", "Generosity", "Perception_of_Corruption", "Positive_Affect", "Negative_Affect")
happiness_decade_2 <- happiness_2 %>%
  filter(Year >= 2013 & Year <= 2023)
happiness_periods <- happiness_decade_2 %>%
  mutate(period = ifelse(Year <= 2017, "Early", "Late")) %>%
  group_by(Country, period) %>%
  summarise(
    avg_life_satisfaction = mean(Life_Satisfaction_Score, na.rm = TRUE),
    avg_gdp = mean(`GDP_per _Capita`, na.rm = TRUE),
    avg_social_support = mean(Social_Support, na.rm = TRUE),
    avg_life_expectancy = mean(Life_Expectancy_at_Birth, na.rm = TRUE),
    avg_freedom = mean(Freedom_to_make_Life_Choices, na.rm = TRUE),
    avg_generosity = mean(Generosity, na.rm = TRUE),
    avg_corruption = mean(Perception_of_Corruption, na.rm = TRUE),
    avg_positive_affect = mean(Positive_Affect, na.rm = TRUE),
    avg_negative_affect = mean(Negative_Affect, na.rm = TRUE),
    .groups = "drop"
  )
happiness_change_factors_2 <- happiness_periods %>%
  pivot_wider(
    names_from = period,
    values_from = c(
      avg_life_satisfaction, avg_gdp, avg_social_support, avg_life_expectancy,
      avg_freedom, avg_generosity, avg_corruption, avg_positive_affect, avg_negative_affect
    )
  ) %>%
  mutate(
    change_life_satisfaction = avg_life_satisfaction_Late - avg_life_satisfaction_Early,
    change_gdp = avg_gdp_Late - avg_gdp_Early,
    change_social_support = avg_social_support_Late - avg_social_support_Early,
    change_life_expectancy = avg_life_expectancy_Late - avg_life_expectancy_Early,
    change_freedom = avg_freedom_Late - avg_freedom_Early,
    change_generosity = avg_generosity_Late - avg_generosity_Early,
    change_corruption = avg_corruption_Late - avg_corruption_Early,
    change_positive_affect = avg_positive_affect_Late - avg_positive_affect_Early,
    change_negative_affect = avg_negative_affect_Late - avg_negative_affect_Early
  )
happiness_change_factors_2 <- happiness_change_factors_2 %>%
  mutate(across(where(is.numeric), ~ ifelse(is.nan(.), NA, .))) %>%
  mutate(across(where(is.numeric), ~ replace_na(., mean(., na.rm = TRUE))))
happiness_change_factors_2 <- happiness_change_factors_2 %>%
  mutate(across(where(is.numeric), ~ round(., 3)))
happiness_change_factors_2 <- happiness_change_factors_2 %>%
  rename(
    `Change in Life Satisfaction` = change_life_satisfaction,
    `Change in GDP per Capita` = change_gdp,
    `Change in Social Support` = change_social_support,
    `Change in Life Expectancy` = change_life_expectancy,
    `Change in Freedom` = change_freedom,
    `Change in Generosity` = change_generosity,
    `Change in Corruption` = change_corruption,
    `Change in Positive Affect` = change_positive_affect,
    `Change in Negative Affect` = change_negative_affect
  )
top_improvers_2 <- happiness_change_factors_2 %>%
  arrange(desc(`Change in Life Satisfaction`)) %>%
  slice_head(n = 10) %>%
  select(Country, `Change in Life Satisfaction`)
top_decliners_2 <- happiness_change_factors_2 %>%
  arrange(`Change in Life Satisfaction`) %>%
  slice_head(n = 10) %>%
  select(Country, `Change in Life Satisfaction`)
top_improvers_2
## # A tibble: 10 × 2
##    Country             `Change in Life Satisfaction`
##    <chr>                                       <dbl>
##  1 Guinea                                      1.15 
##  2 Armenia                                     1.04 
##  3 Serbia                                      0.96 
##  4 Ivory Coast                                 0.926
##  5 Congo (Brazzaville)                         0.903
##  6 Burundi                                     0.87 
##  7 Benin                                       0.86 
##  8 Kosovo                                      0.844
##  9 Niger                                       0.736
## 10 Bulgaria                                    0.72
top_decliners_2
## # A tibble: 10 × 2
##    Country          `Change in Life Satisfaction`
##    <chr>                                    <dbl>
##  1 Lebanon                                 -1.84 
##  2 Afghanistan                             -1.47 
##  3 Sierra Leone                            -1.05 
##  4 Congo (Kinshasa)                        -1.03 
##  5 Jordan                                  -0.907
##  6 Zambia                                  -0.762
##  7 Zimbabwe                                -0.741
##  8 Pakistan                                -0.604
##  9 Turkmenistan                            -0.57 
## 10 Colombia                                -0.512
improved_countries <- top_improvers_2$Country
improved_countries
##  [1] "Guinea"              "Armenia"             "Serbia"             
##  [4] "Ivory Coast"         "Congo (Brazzaville)" "Burundi"            
##  [7] "Benin"               "Kosovo"              "Niger"              
## [10] "Bulgaria"
declined_countries <- top_decliners_2$Country
declined_countries
##  [1] "Lebanon"          "Afghanistan"      "Sierra Leone"     "Congo (Kinshasa)"
##  [5] "Jordan"           "Zambia"           "Zimbabwe"         "Pakistan"        
##  [9] "Turkmenistan"     "Colombia"
happiness_improved_trends <- happiness_decade_2 %>%
  filter(Country %in% improved_countries) 
ggplot(happiness_improved_trends, aes(x = Year, y = Life_Satisfaction_Score, color = Country, group = Country)) +
  geom_line(size = 1.0) +
  geom_point(size = 2) +
  scale_x_continuous(
    breaks = seq(2013, 2023, 1),  # Show every year from 2013 to 2023
    labels = seq(2013, 2023, 1)
  ) +
  labs(
    title = "Top 10 Countries That Improved the Most",
    subtitle = "Changes in Life Satisfaction Score for countries with the largest improvement",
    x = "Year",
    y = "Life Satisfaction Score",
    color = "Country"
  ) +
  theme_minimal(base_size = 13) +
  theme(
    legend.position = "right",
    plot.title = element_text(face = "bold", size = 15, hjust = 0.5),
    plot.subtitle = element_text(size = 11, hjust = 0.5)
  )
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.

happiness_decline_trends <- happiness_decade_2 %>%
  filter(Country %in% declined_countries)
ggplot(happiness_decline_trends, aes(x = Year, y = Life_Satisfaction_Score, color = Country, group = Country)) +
  geom_line(size = 1.0) +
  geom_point(size = 2) +
  scale_x_continuous(
    breaks = seq(2013, 2023, 1),  # Show every year from 2013 to 2023
    labels = seq(2013, 2023, 1)
  ) +
  labs(
    title = "Top 10 Countries That Declined the Most",
    subtitle = "Changes in Life Satisfaction Score for countries with largest decline",
    x = "Year",
    y = "Life Satisfaction Score",
    color = "Country"
  ) +
  theme_minimal(base_size = 13) +
  theme(
    legend.position = "right",
    plot.title = element_text(face = "bold", size = 15, hjust = 0.5),
    plot.subtitle = element_text(size = 11, hjust = 0.5)
  )

4. Based on the previous question, what were some of the factors that improved happiness? What were factors that decreased happiness?

# install.packages("corrr")
library(corrr)
library(dplyr)
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ forcats   1.0.0     ✔ stringr   1.5.2
## ✔ lubridate 1.9.4     ✔ tibble    3.3.0
## ✔ purrr     1.0.4     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter()  masks stats::filter()
## ✖ purrr::flatten() masks jsonlite::flatten()
## ✖ dplyr::lag()     masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
happiness_correlations <- happiness_change_factors_2 %>%
  select(`Change in Life Satisfaction`,
         `Change in GDP per Capita`, `Change in Social Support`,
         `Change in Life Expectancy`, `Change in Freedom`,
         `Change in Generosity`, `Change in Corruption`,
         `Change in Positive Affect`, `Change in Negative Affect`) %>%
  correlate() %>%
  focus(`Change in Life Satisfaction`) %>%
  arrange(desc(`Change in Life Satisfaction`))
## Correlation computed with
## • Method: 'pearson'
## • Missing treated using: 'pairwise.complete.obs'
happiness_correlations
## # A tibble: 8 × 2
##   term                      `Change in Life Satisfaction`
##   <chr>                                             <dbl>
## 1 Change in Positive Affect                        0.637 
## 2 Change in Freedom                                0.327 
## 3 Change in Social Support                         0.326 
## 4 Change in Generosity                             0.273 
## 5 Change in GDP per Capita                         0.194 
## 6 Change in Life Expectancy                        0.0541
## 7 Change in Corruption                            -0.180 
## 8 Change in Negative Affect                       -0.297
library(tibble)
improving_factors <- happiness_correlations %>%
  filter(`Change in Life Satisfaction` > 0) %>%
  arrange(desc(`Change in Life Satisfaction`))
declining_factors <- happiness_correlations %>%
  filter(`Change in Life Satisfaction` < 0) %>%
  arrange(desc(`Change in Life Satisfaction`))
improving_factors
## # A tibble: 6 × 2
##   term                      `Change in Life Satisfaction`
##   <chr>                                             <dbl>
## 1 Change in Positive Affect                        0.637 
## 2 Change in Freedom                                0.327 
## 3 Change in Social Support                         0.326 
## 4 Change in Generosity                             0.273 
## 5 Change in GDP per Capita                         0.194 
## 6 Change in Life Expectancy                        0.0541
declining_factors
## # A tibble: 2 × 2
##   term                      `Change in Life Satisfaction`
##   <chr>                                             <dbl>
## 1 Change in Corruption                             -0.180
## 2 Change in Negative Affect                        -0.297