Library Used

library(tidyverse)
library(reshape2)

Data Setup

IQ_country <-  read.csv("avgIQpercountry.csv")
IQ_classification <- read.csv("iq_classification.csv",sep = ";")

let’s glance at the data

head(IQ_country, n = 10)
##    Rank       Country Average.IQ Continent Literacy.Rate Nobel.Prices
## 1     1         Japan     106.48      Asia          0.99           29
## 2     2        Taiwan     106.47      Asia          0.96            4
## 3     3     Singapore     105.89      Asia          0.97            0
## 4     4     Hong Kong     105.37      Asia          0.94            1
## 5     5         China     104.10      Asia          0.96            8
## 6     6   South Korea     102.35      Asia          0.98            0
## 7     7       Belarus     101.60    Europe          1.00            2
## 8     8       Finland     101.20    Europe          1.00            5
## 9     9 Liechtenstein     101.07    Europe          1.00            0
## 10   10       Germany     100.74    Europe          0.99          111
##    HDI..2021. Mean.years.of.schooling...2021 GNI...2021 Population...2023
## 1       0.925                           13.4      42274         123294513
## 2          NA                             NA         NA          10143543
## 3       0.939                           11.9      90919           6014723
## 4       0.952                           12.2      62607           7491609
## 5       0.768                            7.6      17504        1425671352
## 6       0.925                           12.5      44501          51784059
## 7       0.808                           12.1      18849           9498238
## 8       0.940                           12.9      49452           5545475
## 9       0.935                           12.5     146830           \t39315
## 10      0.942                           14.1      54534          83294633
IQ_classification
##    IQ.range                      Classification Percentile....
## 1      180-                              Genius         <0.01%
## 2   160-179                       Almost genius          0.01%
## 3   145-159                         Very gifted           0.1%
## 4   130-144                              Gifted           2.3%
## 5  120-129                    Moderately gifted           6.7%
## 6   110-119                       Above average          16.1%
## 7    90-109                Average intelligence          50.0%
## 8     85-89                       Below average          16.1%
## 9     70-84 Borderline intellectual functioning           2.3%
## 10    50-69        Mild intellectual disability           2.3%
## 11    35-49    Moderate intellectual disability           1.0%
## 12    20-34      Severe intellectual disability           0.5%
## 13      -20    Profound intellectual disability          <0.1%

Find NA values

At a glance it looks like there’s some missing data, let’s look at that more closely

sapply(IQ_country, function(x) sum(is.na(x)))
##                           Rank                        Country 
##                              0                              0 
##                     Average.IQ                      Continent 
##                              0                              0 
##                  Literacy.Rate                   Nobel.Prices 
##                              0                              0 
##                     HDI..2021. Mean.years.of.schooling...2021 
##                             14                             14 
##                     GNI...2021              Population...2023 
##                             14                              0

Data Cleaning

Rename the columns for better readability

colnames(IQ_country) <- c("Rank","Country", "Average_IQ","Continent", "Literacy_Rate","Nobel_Prizes", "HDI_2021","Mean_years_of_schooling_2021","GNI_2021","Population_2023")

With the existence of missing data, let’s just remove them

IQ_country_clean <- drop_na(IQ_country)

Turn Population data(strings) into numerical data

IQ_country_clean$Population_2023 <- as.numeric(IQ_country_clean$Population_2023)

Top 20 Country Average IQ

Seems like the data is already sorted based on average iq, let’s look at the top 20. We’ll use the clean version

print(IQ_country_clean[1:20,c("Country","Average_IQ")])
##           Country Average_IQ
## 1           Japan     106.48
## 2       Singapore     105.89
## 3       Hong Kong     105.37
## 4           China     104.10
## 5     South Korea     102.35
## 6         Belarus     101.60
## 7         Finland     101.20
## 8   Liechtenstein     101.07
## 9         Germany     100.74
## 10    Netherlands     100.74
## 11        Estonia     100.72
## 12     Luxembourg      99.87
## 13       Cambodia      99.75
## 14         Canada      99.52
## 15      Australia      99.24
## 16        Hungary      99.24
## 17    Switzerland      99.24
## 18 United Kingdom      99.12
## 19       Slovenia      98.60
## 20    New Zealand      98.57

Visualizing top 20 Country Average IQ

Top_IQ <- IQ_country_clean[1:20,c("Country","Average_IQ")]
ggplot(data=Top_IQ, aes(x=reorder(Country,Average_IQ), y=Average_IQ,fill = Country)) +
    geom_bar(colour = "black",stat="identity")+
    guides(fill = FALSE) +
    ylab("Mean IQ") +
    xlab("Country") +
    coord_flip()
## Warning: The `<scale>` argument of `guides()` cannot be `FALSE`. Use "none" instead as
## of ggplot2 3.3.4.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.

Looking into the countries with the most nobel contribution

Find Countries with the most nobel prizes

Nobel <- IQ_country_clean[,c("Country","Nobel_Prizes")]
Nobel <- Nobel[order(Nobel$Nobel_Prizes,decreasing = TRUE),]
Top_Nobel <- Nobel[1:20,]
Top_Nobel
##            Country Nobel_Prizes
## 25   United States          400
## 18  United Kingdom          137
## 9          Germany          111
## 28          France           71
## 27          Sweden           32
## 1            Japan           29
## 14          Canada           28
## 17     Switzerland           27
## 10     Netherlands           22
## 21         Austria           22
## 38           Italy           21
## 29          Poland           19
## 16         Hungary           13
## 23         Denmark           13
## 26          Norway           13
## 43          Israel           13
## 15       Australia           12
## 127          India           12
## 24         Belgium           11
## 35         Ireland           11

Visualize

ggplot(data=Top_Nobel, aes(x=reorder(Country,Nobel_Prizes), y=Nobel_Prizes,fill = Country)) +
    geom_bar(colour = "black",stat="identity")+
    guides(fill = FALSE) +
    ylab("Nobel Prizes") +
    xlab("Country") +
    coord_flip()

Visualizing Each Variable in Histogram

Turn Wide Data to Long Data

selected_columns <- c("Average_IQ", "Literacy_Rate", "HDI_2021","Mean_years_of_schooling_2021","GNI_2021")

data_long <- IQ_country_clean %>%
  select(all_of(selected_columns)) %>%
  gather(key = "Variable", value = "Value")

Plot data into histogram

ggplot(data_long, aes(x = Value, fill = Variable)) +
  geom_histogram(position = "identity", alpha = 0.7,bins = 10) +
  facet_wrap(~ Variable, scales = "free") +
  theme(axis.text = element_text(size = 7),
        strip.text = element_text(size = 6)) +
  ggtitle("Grid of Histograms for Features")

Visualizing Mean IQ per Continent

Group data by continent

Continent_mean <- aggregate(x = IQ_country_clean$Average_IQ,by = list(IQ_country_clean$Continent),FUN = mean)

colnames(Continent_mean) <- c("Continent","Mean_IQ")

print(Continent_mean)
##         Continent  Mean_IQ
## 1          Africa 68.79347
## 2            Asia 84.88659
## 3 Central America 73.81000
## 4          Europe 94.90488
## 5     Europe/Asia 89.19667
## 6   North America 94.89333
## 7         Oceania 88.25750
## 8   South America 83.78333

Plot on a Horizontal Boxplot

Horizontal for easier read

ggplot(Continent_mean,aes (x = Mean_IQ, y = Continent,fill = Continent)) +
  geom_bar(stat ="identity",aes(y = Continent))

Visualizing Scatter Plot Literacy Rate vs Average IQ + Regression Line

Create Regression Line first

reg <- lm(formula = Average_IQ ~ Literacy_Rate, data = IQ_country_clean)

coeff <- coefficients(reg)
intercept <- coeff[1]
slope <- coeff[2]

Set up the plot

plot <- ggplot(IQ_country_clean,aes(x = Literacy_Rate, y = Average_IQ)) + geom_point() + labs(x = "Literacy Rate", y = "Average IQ") + geom_point()

plot + geom_abline(intercept = intercept,slope = slope,color = "blue",linewidth = 0.5, linetype = "dashed")

Visualizing Scatter Plot HDI 2021 vs Average IQ + Regression Line

Create the linear model

reg <- lm(formula = Average_IQ ~ HDI_2021 , data = IQ_country_clean)

coeff <- coefficients(reg)
intercept <- coeff[1]
slope <- coeff[2]

Visualize

plot <- ggplot(IQ_country_clean,aes(x = HDI_2021, y = Average_IQ)) + geom_point() + labs(x = "HDI 2021", y = "Average IQ")
plot + geom_abline(intercept = intercept,slope = slope,color = "red",linewidth = 0.5, linetype = "dashed")

Looking At the correlation and visualize with heatmap

Let’s make the correlation matrix first, choosing the appropriate columns

Columns <- c("Average_IQ","Nobel_Prizes","HDI_2021","Mean_years_of_schooling_2021","GNI_2021","Population_2023")

Corr_data <- IQ_country_clean[,Columns]

Corr <- round(cor(Corr_data),2)

melted_corr_mat <- melt(Corr)

Visualize into heatmap

ggplot(melted_corr_mat,aes(x=Var1,y=Var2,fill=value)) + 
  geom_tile() +
  theme(axis.text = element_text(size = 6.5)) +
  scale_x_discrete(guide = guide_axis(n.dodge = 2)) +
  geom_text(aes(Var2, Var1, label = value), 
          color = "white", size = 4) +
  NULL