My R skills are pretty basic, and I like simple code, so I will try to keep the code as simple as possible, possibly at the expense of brevity. I will also check my results at every step to make sure I’m getting the desired result.

Let’s bring in the original TB dataset, and look at the first few rows. Admittedly, I had to add a header row to the original dataset, as the Afghan women of 1995 were getting munched up when they were the first row.

tb <- read.csv(url("https://raw.githubusercontent.com/AsherMeyers/DATA-607/master/tb.csv"))
head(tb)
##       country year    sex child adult elderly
## 1 Afghanistan 1995 female    -1    -1      -1
## 2 Afghanistan 1995   male    -1    -1      -1
## 3 Afghanistan 1996 female    -1    -1      -1
## 4 Afghanistan 1996   male    -1    -1      -1
## 5 Afghanistan 1997 female     5    96       1
## 6 Afghanistan 1997   male     0    26       0

Column headers are missing. So first, we’ll add column names

names(tb) = c("country","year","sex","child","adult","elderly")
head(tb)
##       country year    sex child adult elderly
## 1 Afghanistan 1995 female    -1    -1      -1
## 2 Afghanistan 1995   male    -1    -1      -1
## 3 Afghanistan 1996 female    -1    -1      -1
## 4 Afghanistan 1996   male    -1    -1      -1
## 5 Afghanistan 1997 female     5    96       1
## 6 Afghanistan 1997   male     0    26       0

We see there are negative values, -1, which is impossible. If you look at the original dataset, you see these values were null. So we’ll convert those negative values to zero.

tb[tb=="-1"] <- 0 # Replace every value of -1 with 0
head(tb)
##       country year    sex child adult elderly
## 1 Afghanistan 1995 female     0     0       0
## 2 Afghanistan 1995   male     0     0       0
## 3 Afghanistan 1996 female     0     0       0
## 4 Afghanistan 1996   male     0     0       0
## 5 Afghanistan 1997 female     5    96       1
## 6 Afghanistan 1997   male     0    26       0

Let’s scan the country names to make sure everything is spelled correctly:

unique(tb$country)
##   [1] Afghanistan                                         
##   [2] Algeria                                             
##   [3] Angola                                              
##   [4] Argentina                                           
##   [5] Azerbaijan                                          
##   [6] Bangladesh                                          
##   [7] Belarus                                             
##   [8] Benin                                               
##   [9] Bolivia (Plurinational State of)                    
##  [10] Botswana                                            
##  [11] Brazil                                              
##  [12] Burkina Faso                                        
##  [13] Burundi                                             
##  [14] Cambodia                                            
##  [15] Cameroon                                            
##  [16] Central African Republic                            
##  [17] Chad                                                
##  [18] Chile                                               
##  [19] China                                               
##  [20] China, Hong Kong SAR                                
##  [21] Colombia                                            
##  [22] Congo                                               
##  [23] Côte d'Ivoire                                      
##  [24] Democratic People's Republic of Korea               
##  [25] Democratic Republic of the Congo                    
##  [26] Djibouti                                            
##  [27] Dominican Republic                                  
##  [28] Ecuador                                             
##  [29] Egypt                                               
##  [30] Ethiopia                                            
##  [31] France                                              
##  [32] Georgia                                             
##  [33] Germany                                             
##  [34] Ghana                                               
##  [35] Guatemala                                           
##  [36] Guinea                                              
##  [37] Haiti                                               
##  [38] Honduras                                            
##  [39] India                                               
##  [40] Indonesia                                           
##  [41] Iran (Islamic Republic of)                          
##  [42] Iraq                                                
##  [43] Italy                                               
##  [44] Japan                                               
##  [45] Kazakhstan                                          
##  [46] Kenya                                               
##  [47] Kyrgyzstan                                          
##  [48] Lao People's Democratic Republic                    
##  [49] Lesotho                                             
##  [50] Liberia                                             
##  [51] Madagascar                                          
##  [52] Malawi                                              
##  [53] Malaysia                                            
##  [54] Mali                                                
##  [55] Mexico                                              
##  [56] Mongolia                                            
##  [57] Morocco                                             
##  [58] Mozambique                                          
##  [59] Myanmar                                             
##  [60] Namibia                                             
##  [61] Nepal                                               
##  [62] Nicaragua                                           
##  [63] Niger                                               
##  [64] Nigeria                                             
##  [65] Pakistan                                            
##  [66] Paraguay                                            
##  [67] Peru                                                
##  [68] Philippines                                         
##  [69] Poland                                              
##  [70] Portugal                                            
##  [71] Republic of Korea                                   
##  [72] Republic of Moldova                                 
##  [73] Romania                                             
##  [74] Russian Federation                                  
##  [75] Rwanda                                              
##  [76] Saudi Arabia                                        
##  [77] Senegal                                             
##  [78] Sierra Leone                                        
##  [79] Somalia                                             
##  [80] South Africa                                        
##  [81] Spain                                               
##  [82] Sri Lanka                                           
##  [83] Sudan                                               
##  [84] Swaziland                                           
##  [85] Syrian Arab Republic                                
##  [86] Tajikistan                                          
##  [87] Thailand                                            
##  [88] Togo                                                
##  [89] Turkey                                              
##  [90] Uganda                                              
##  [91] Ukraine                                             
##  [92] United Kingdom of Great Britain and Northern Ireland
##  [93] United Republic of Tanzania                         
##  [94] United States of America                            
##  [95] Uzbekistan                                          
##  [96] Venezuela (Bolivarian Republic of)                  
##  [97] Viet Nam                                            
##  [98] Yemen                                               
##  [99] Zambia                                              
## [100] Zimbabwe                                            
## 100 Levels: Afghanistan Algeria Angola Argentina Azerbaijan ... Zimbabwe

We see that Côte d’Ivoire is spelled as Côte d’Ivoire. Let’s fix that.

tb$country <- as.character(tb$country)
tb$country[tb$country == "Côte d'Ivoire"] <- "Côte d'Ivoire"

unique(tb$country)
##   [1] "Afghanistan"                                         
##   [2] "Algeria"                                             
##   [3] "Angola"                                              
##   [4] "Argentina"                                           
##   [5] "Azerbaijan"                                          
##   [6] "Bangladesh"                                          
##   [7] "Belarus"                                             
##   [8] "Benin"                                               
##   [9] "Bolivia (Plurinational State of)"                    
##  [10] "Botswana"                                            
##  [11] "Brazil"                                              
##  [12] "Burkina Faso"                                        
##  [13] "Burundi"                                             
##  [14] "Cambodia"                                            
##  [15] "Cameroon"                                            
##  [16] "Central African Republic"                            
##  [17] "Chad"                                                
##  [18] "Chile"                                               
##  [19] "China"                                               
##  [20] "China, Hong Kong SAR"                                
##  [21] "Colombia"                                            
##  [22] "Congo"                                               
##  [23] "Côte d'Ivoire"                                       
##  [24] "Democratic People's Republic of Korea"               
##  [25] "Democratic Republic of the Congo"                    
##  [26] "Djibouti"                                            
##  [27] "Dominican Republic"                                  
##  [28] "Ecuador"                                             
##  [29] "Egypt"                                               
##  [30] "Ethiopia"                                            
##  [31] "France"                                              
##  [32] "Georgia"                                             
##  [33] "Germany"                                             
##  [34] "Ghana"                                               
##  [35] "Guatemala"                                           
##  [36] "Guinea"                                              
##  [37] "Haiti"                                               
##  [38] "Honduras"                                            
##  [39] "India"                                               
##  [40] "Indonesia"                                           
##  [41] "Iran (Islamic Republic of)"                          
##  [42] "Iraq"                                                
##  [43] "Italy"                                               
##  [44] "Japan"                                               
##  [45] "Kazakhstan"                                          
##  [46] "Kenya"                                               
##  [47] "Kyrgyzstan"                                          
##  [48] "Lao People's Democratic Republic"                    
##  [49] "Lesotho"                                             
##  [50] "Liberia"                                             
##  [51] "Madagascar"                                          
##  [52] "Malawi"                                              
##  [53] "Malaysia"                                            
##  [54] "Mali"                                                
##  [55] "Mexico"                                              
##  [56] "Mongolia"                                            
##  [57] "Morocco"                                             
##  [58] "Mozambique"                                          
##  [59] "Myanmar"                                             
##  [60] "Namibia"                                             
##  [61] "Nepal"                                               
##  [62] "Nicaragua"                                           
##  [63] "Niger"                                               
##  [64] "Nigeria"                                             
##  [65] "Pakistan"                                            
##  [66] "Paraguay"                                            
##  [67] "Peru"                                                
##  [68] "Philippines"                                         
##  [69] "Poland"                                              
##  [70] "Portugal"                                            
##  [71] "Republic of Korea"                                   
##  [72] "Republic of Moldova"                                 
##  [73] "Romania"                                             
##  [74] "Russian Federation"                                  
##  [75] "Rwanda"                                              
##  [76] "Saudi Arabia"                                        
##  [77] "Senegal"                                             
##  [78] "Sierra Leone"                                        
##  [79] "Somalia"                                             
##  [80] "South Africa"                                        
##  [81] "Spain"                                               
##  [82] "Sri Lanka"                                           
##  [83] "Sudan"                                               
##  [84] "Swaziland"                                           
##  [85] "Syrian Arab Republic"                                
##  [86] "Tajikistan"                                          
##  [87] "Thailand"                                            
##  [88] "Togo"                                                
##  [89] "Turkey"                                              
##  [90] "Uganda"                                              
##  [91] "Ukraine"                                             
##  [92] "United Kingdom of Great Britain and Northern Ireland"
##  [93] "United Republic of Tanzania"                         
##  [94] "United States of America"                            
##  [95] "Uzbekistan"                                          
##  [96] "Venezuela (Bolivarian Republic of)"                  
##  [97] "Viet Nam"                                            
##  [98] "Yemen"                                               
##  [99] "Zambia"                                              
## [100] "Zimbabwe"

Now it’s fixed. This is important, because we will be sorting the data using spelling, so the spelling must be correct.

NB: the method for finding and replacing the erroneous country name is from here: http://stackoverflow.com/questions/5824173/replace-a-value-in-a-data-frame-based-on-a-conditional-if-statement-in-r

Now, we want to know the overall rate of TB, so we’ll have to combine all the demographic groups into one field. First, we’ll keep the sexes separate, but combine the different age groups.

We compile a column that sums the child, adult and elderly cases, and put that column in.

cases <- tb$child + tb$adult + tb$elderly
names(cases) <- c("cases")
tb <- data.frame(tb,cases)
head(tb)
##       country year    sex child adult elderly cases
## 1 Afghanistan 1995 female     0     0       0     0
## 2 Afghanistan 1995   male     0     0       0     0
## 3 Afghanistan 1996 female     0     0       0     0
## 4 Afghanistan 1996   male     0     0       0     0
## 5 Afghanistan 1997 female     5    96       1   102
## 6 Afghanistan 1997   male     0    26       0    26

Now, we sum the sexes, so that each row represents a single year. We separate the data frames into male and female dataframes, and then sum the cases of those respective dataframes.

Then we take the country and year from one of the sex-specific dataframes - it doesn’t matter which sex, we just don’t want the original dataset with both sexes, because that one has two rows for each country-year. I used the male dataset in the code below.

tbmales <- subset(tb, tb$sex == "male")
tbfemales <- subset(tb, tb$sex == "female")
tbcasesall <- tbmales$cases + tbfemales$cases
tball <- data.frame(tbmales$country,tbmales$year,tbcasesall)
names(tball) <- c("country","year","cases")
head(tball)
##       country year cases
## 1 Afghanistan 1995     0
## 2 Afghanistan 1996     0
## 3 Afghanistan 1997   128
## 4 Afghanistan 1998  1778
## 5 Afghanistan 1999   745
## 6 Afghanistan 2000  2666

Now we need to figure out a rate for TB. To do that, we’ll need the population at the given time. So we’ll import the population data.

population <- read.csv(url("https://raw.githubusercontent.com/AsherMeyers/DATA-607/master/population.csv"))
head(population)
##       country year population
## 1 Afghanistan 1995   17586073
## 2     Algeria 1995   29315463
## 3      Angola 1995   12104952
## 4   Argentina 1995   34833168
## 5  Azerbaijan 1995    7770806
## 6  Bangladesh 1995  119869585
str(population)
## 'data.frame':    1900 obs. of  3 variables:
##  $ country   : Factor w/ 100 levels "Afghanistan",..: 1 2 3 4 5 6 7 8 9 10 ...
##  $ year      : int  1995 1995 1995 1995 1995 1995 1995 1995 1995 1995 ...
##  $ population: int  17586073 29315463 12104952 34833168 7770806 119869585 10189075 5985658 7635362 1583453 ...

Let’s check the population dataset for errors:

unique(population$country)
##   [1] Afghanistan                                         
##   [2] Algeria                                             
##   [3] Angola                                              
##   [4] Argentina                                           
##   [5] Azerbaijan                                          
##   [6] Bangladesh                                          
##   [7] Belarus                                             
##   [8] Benin                                               
##   [9] Bolivia (Plurinational State of)                    
##  [10] Botswana                                            
##  [11] Brazil                                              
##  [12] Burkina Faso                                        
##  [13] Burundi                                             
##  [14] Cambodia                                            
##  [15] Cameroon                                            
##  [16] Central African Republic                            
##  [17] Chad                                                
##  [18] Chile                                               
##  [19] China                                               
##  [20] China, Hong Kong SAR                                
##  [21] Colombia                                            
##  [22] Congo                                               
##  [23] Côte d'Ivoire                                      
##  [24] Democratic People's Republic of Korea               
##  [25] Democratic Republic of the Congo                    
##  [26] Djibouti                                            
##  [27] Dominican Republic                                  
##  [28] Ecuador                                             
##  [29] Egypt                                               
##  [30] Ethiopia                                            
##  [31] France                                              
##  [32] Georgia                                             
##  [33] Germany                                             
##  [34] Ghana                                               
##  [35] Guatemala                                           
##  [36] Guinea                                              
##  [37] Haiti                                               
##  [38] Honduras                                            
##  [39] India                                               
##  [40] Indonesia                                           
##  [41] Iran (Islamic Republic of)                          
##  [42] Iraq                                                
##  [43] Italy                                               
##  [44] Japan                                               
##  [45] Kazakhstan                                          
##  [46] Kenya                                               
##  [47] Kyrgyzstan                                          
##  [48] Lao People's Democratic Republic                    
##  [49] Lesotho                                             
##  [50] Liberia                                             
##  [51] Madagascar                                          
##  [52] Malawi                                              
##  [53] Malaysia                                            
##  [54] Mali                                                
##  [55] Mexico                                              
##  [56] Mongolia                                            
##  [57] Morocco                                             
##  [58] Mozambique                                          
##  [59] Myanmar                                             
##  [60] Namibia                                             
##  [61] Nepal                                               
##  [62] Nicaragua                                           
##  [63] Niger                                               
##  [64] Nigeria                                             
##  [65] Pakistan                                            
##  [66] Paraguay                                            
##  [67] Peru                                                
##  [68] Philippines                                         
##  [69] Poland                                              
##  [70] Portugal                                            
##  [71] Republic of Korea                                   
##  [72] Republic of Moldova                                 
##  [73] Romania                                             
##  [74] Russian Federation                                  
##  [75] Rwanda                                              
##  [76] Saudi Arabia                                        
##  [77] Senegal                                             
##  [78] Sierra Leone                                        
##  [79] Somalia                                             
##  [80] South Africa                                        
##  [81] Spain                                               
##  [82] Sri Lanka                                           
##  [83] Sudan                                               
##  [84] Swaziland                                           
##  [85] Syrian Arab Republic                                
##  [86] Tajikistan                                          
##  [87] Thailand                                            
##  [88] Togo                                                
##  [89] Turkey                                              
##  [90] Uganda                                              
##  [91] Ukraine                                             
##  [92] United Kingdom of Great Britain and Northern Ireland
##  [93] United Republic of Tanzania                         
##  [94] United States of America                            
##  [95] Uzbekistan                                          
##  [96] Venezuela (Bolivarian Republic of)                  
##  [97] Viet Nam                                            
##  [98] Yemen                                               
##  [99] Zambia                                              
## [100] Zimbabwe                                            
## 100 Levels: Afghanistan Algeria Angola Argentina Azerbaijan ... Zimbabwe

Again, we see that Ivory Coast has a corrupted name. We’ll fix that

population$country <- as.character(population$country)
population$country[population$country == "Côte d'Ivoire"] <- "Côte d'Ivoire"

We see the population data is sorted a bit differently than our TB dataset - the TB dataset is sorted by country, then year, while the population dataset is sorted by year, then country. Before we can combine the two datasets, we have to align them.

population <- population[order(population$country, population$year),]
head(population)
##         country year population
## 1   Afghanistan 1995   17586073
## 101 Afghanistan 1996   18415307
## 201 Afghanistan 1997   19021226
## 301 Afghanistan 1998   19496836
## 401 Afghanistan 1999   19987071
## 501 Afghanistan 2000   20595360

Let’s add the population column to our tball dataframe:

tball <- data.frame(tball, population$population)
names(tball) <- c("country", "year", "cases", "population")
head(tball)
##       country year cases population
## 1 Afghanistan 1995     0   17586073
## 2 Afghanistan 1996     0   18415307
## 3 Afghanistan 1997   128   19021226
## 4 Afghanistan 1998  1778   19496836
## 5 Afghanistan 1999   745   19987071
## 6 Afghanistan 2000  2666   20595360

Now we’ll add our rate column: Simply a column that is cases divided by population. We will multiply by 100,000, so that the rate will be in the units of TB cases per 100,000 people, per year. And we’ll round it to two decimals.

rate <- round((10^5)*tball$cases/tball$population,2)
tball <- data.frame(tball, rate)
names(tball) <- c("country", "year", "cases", "population", "rate")
head(tball)
##       country year cases population  rate
## 1 Afghanistan 1995     0   17586073  0.00
## 2 Afghanistan 1996     0   18415307  0.00
## 3 Afghanistan 1997   128   19021226  0.67
## 4 Afghanistan 1998  1778   19496836  9.12
## 5 Afghanistan 1999   745   19987071  3.73
## 6 Afghanistan 2000  2666   20595360 12.94

Given that even a country like the US has thousands of TB cases a year, odds are that any country reporting zero cases is simply not telling the truth - either out of ignorance (not recording the data) or malice (concealing the data to give a benign image). Let’s trim out the data points that report zero cases. Of course, we changed lots of values to zero in the beginning, from -1, which presumably meant null value.

tball <- subset(tball, tball$rate != 0)

Let’s see which country and year had the highest TB rate

tball[which.max(tball$rate),]
##        country year cases population   rate
## 1593 Swaziland 2010  9706    1193148 813.48

Average TB rates by country, alphabetized:

tbcountry <- round(tapply(tball$rate, tball$country, mean),1)
tbcountry
##                                          Afghanistan 
##                                                 31.5 
##                                              Algeria 
##                                                 23.3 
##                                               Angola 
##                                                 95.8 
##                                            Argentina 
##                                                 16.8 
##                                           Azerbaijan 
##                                                 20.0 
##                                           Bangladesh 
##                                                 54.9 
##                                              Belarus 
##                                                 30.0 
##                                                Benin 
##                                                 32.0 
##                     Bolivia (Plurinational State of) 
##                                                 73.9 
##                                             Botswana 
##                                                235.0 
##                                               Brazil 
##                                                 31.8 
##                                         Burkina Faso 
##                                                 15.6 
##                                              Burundi 
##                                                 46.2 
##                                             Cambodia 
##                                                126.1 
##                                             Cameroon 
##                                                 48.2 
##                             Central African Republic 
##                                                 88.2 
##                                                 Chad 
##                                                 44.2 
##                                                Chile 
##                                                 10.9 
##                                                China 
##                                                 33.1 
##                                 China, Hong Kong SAR 
##                                                 51.0 
##                                             Colombia 
##                                                 21.4 
##                                                Congo 
##                                                 91.3 
##                                        Côte d'Ivoire 
##                                                 68.4 
##                Democratic People's Republic of Korea 
##                                                154.6 
##                     Democratic Republic of the Congo 
##                                                 91.2 
##                                             Djibouti 
##                                                188.6 
##                                   Dominican Republic 
##                                                 27.3 
##                                              Ecuador 
##                                                 24.3 
##                                                Egypt 
##                                                  8.9 
##                                             Ethiopia 
##                                                 40.4 
##                                               France 
##                                                  4.7 
##                                              Georgia 
##                                                 49.4 
##                                              Germany 
##                                                  3.6 
##                                                Ghana 
##                                                 38.6 
##                                            Guatemala 
##                                                 18.7 
##                                               Guinea 
##                                                 53.3 
##                                                Haiti 
##                                                103.2 
##                                             Honduras 
##                                                 36.9 
##                                                India 
##                                                 34.1 
##                                            Indonesia 
##                                                 69.0 
##                           Iran (Islamic Republic of) 
##                                                 10.6 
##                                                 Iraq 
##                                                 26.5 
##                                                Italy 
##                                                  3.2 
##                                                Japan 
##                                                 12.8 
##                                           Kazakhstan 
##                                                 83.1 
##                                                Kenya 
##                                                125.3 
##                                           Kyrgyzstan 
##                                                 49.2 
##                     Lao People's Democratic Republic 
##                                                 40.5 
##                                              Lesotho 
##                                                261.9 
##                                              Liberia 
##                                                 90.1 
##                                           Madagascar 
##                                                 77.0 
##                                               Malawi 
##                                                 64.9 
##                                             Malaysia 
##                                                 49.4 
##                                                 Mali 
##                                                 28.7 
##                                               Mexico 
##                                                 13.2 
##                                             Mongolia 
##                                                 80.0 
##                                              Morocco 
##                                                 62.4 
##                                           Mozambique 
##                                                 65.2 
##                                              Myanmar 
##                                                 55.0 
##                                              Namibia 
##                                                235.8 
##                                                Nepal 
##                                                 54.9 
##                                            Nicaragua 
##                                                 29.3 
##                                                Niger 
##                                                 34.5 
##                                              Nigeria 
##                                                 23.5 
##                                             Pakistan 
##                                                 31.6 
##                                             Paraguay 
##                                                 24.9 
##                                                 Peru 
##                                                 65.5 
##                                          Philippines 
##                                                 70.2 
##                                               Poland 
##                                                 13.3 
##                                             Portugal 
##                                                 20.3 
##                                    Republic of Korea 
##                                                 46.2 
##                                  Republic of Moldova 
##                                                 55.6 
##                                              Romania 
##                                                 62.4 
##                                   Russian Federation 
##                                                 37.9 
##                                               Rwanda 
##                                                 37.6 
##                                         Saudi Arabia 
##                                                 10.7 
##                                              Senegal 
##                                                 59.7 
##                                         Sierra Leone 
##                                                 90.3 
##                                              Somalia 
##                                                 64.5 
##                                         South Africa 
##                                                398.5 
##                                                Spain 
##                                                  9.5 
##                                            Sri Lanka 
##                                                 30.1 
##                                                Sudan 
##                                                 32.4 
##                                            Swaziland 
##                                                325.3 
##                                 Syrian Arab Republic 
##                                                 12.5 
##                                           Tajikistan 
##                                                 43.4 
##                                             Thailand 
##                                                 38.6 
##                                                 Togo 
##                                                 29.6 
##                                               Turkey 
##                                                 18.7 
##                                               Uganda 
##                                                 71.8 
##                                              Ukraine 
##                                                 36.2 
## United Kingdom of Great Britain and Northern Ireland 
##                                                  7.0 
##                          United Republic of Tanzania 
##                                                 75.2 
##                             United States of America 
##                                                  2.8 
##                                           Uzbekistan 
##                                                 36.5 
##                   Venezuela (Bolivarian Republic of) 
##                                                 18.8 
##                                             Viet Nam 
##                                                 63.7 
##                                                Yemen 
##                                                 22.4 
##                                               Zambia 
##                                                164.3 
##                                             Zimbabwe 
##                                                229.9

Top 10 TB rates by country:

tbcountry <- sort(tbcountry, decreasing = TRUE)
head(tbcountry, 10)
##                          South Africa 
##                                 398.5 
##                             Swaziland 
##                                 325.3 
##                               Lesotho 
##                                 261.9 
##                               Namibia 
##                                 235.8 
##                              Botswana 
##                                 235.0 
##                              Zimbabwe 
##                                 229.9 
##                              Djibouti 
##                                 188.6 
##                                Zambia 
##                                 164.3 
## Democratic People's Republic of Korea 
##                                 154.6 
##                              Cambodia 
##                                 126.1
tbcountrynames <- names(tbcountry)
plot(tbcountry[1:10], xlim = c(1,12), ylim = c(100, 350))
text(tbcountry[1:10], row.names(tbcountry[1:10]), cex=0.8, pos=4, col="red")

Average TB rates by year:

tbyear <- round(tapply(tball$rate, tball$year, mean),1)
plot(1995:2013, tbyear, main = "Average TB Rate By Year", ylim = c(0,100))

Analysis:

In studying the TB rates, we can think of a few obvious questions.

Who is getting infected most? Sub-Saharan African countries top the list, with poor Asian countries not far behind.

head(tbcountry, 20)
##                          South Africa 
##                                 398.5 
##                             Swaziland 
##                                 325.3 
##                               Lesotho 
##                                 261.9 
##                               Namibia 
##                                 235.8 
##                              Botswana 
##                                 235.0 
##                              Zimbabwe 
##                                 229.9 
##                              Djibouti 
##                                 188.6 
##                                Zambia 
##                                 164.3 
## Democratic People's Republic of Korea 
##                                 154.6 
##                              Cambodia 
##                                 126.1 
##                                 Kenya 
##                                 125.3 
##                                 Haiti 
##                                 103.2 
##                                Angola 
##                                  95.8 
##                                 Congo 
##                                  91.3 
##      Democratic Republic of the Congo 
##                                  91.2 
##                          Sierra Leone 
##                                  90.3 
##                               Liberia 
##                                  90.1 
##              Central African Republic 
##                                  88.2 
##                            Kazakhstan 
##                                  83.1 
##                              Mongolia 
##                                  80.0

Is it a matter of geography or economy? It’s not clear from the data - both would seem to be factors; tropical climes are probably favorable to disease, and so is poverty. The poor of the world often cannot afford good sanitation; its absence contributes to the spread of disease.

Why was there a sudden global increase in the rates of infection? TB after all is not a new disease. It may mostly be an artifact of data collection - perhaps countries got more serious about tracking infections. Many were not recording infections at all in the earlier years, and heir nascent reporting infrastructure may have undercounted relative to later years, i.e. they were ‘just getting started.’ Given the gaps in reporting, the data on its own is not very credible - one should probably confer with real life experts with hands on experience to gauge whether what change if any there is in infections. That said, the data is probably reliable enough to make order of magnitude comparisons - eg that infections are far more likely in a place like South Africa than the US.

Lastly, here is a list of the difference between the maximum and minimum infection rates in the period studied. Many of the differences seem too great to be explained by a real change in TB prevalence.

tbmax <- round(tapply(tball$rate, tball$country, max),1)
tbmin <- round(tapply(tball$rate, tball$country, min),1)
tbdiff <- sort(tbmax - tbmin, decreasing = TRUE)
tbdiff
##                                            Swaziland 
##                                                745.0 
##                                         South Africa 
##                                                732.3 
##                                              Lesotho 
##                                                539.9 
##                                              Namibia 
##                                                395.6 
##                Democratic People's Republic of Korea 
##                                                362.5 
##                                             Botswana 
##                                                251.3 
##                                             Zimbabwe 
##                                                249.2 
##                                             Djibouti 
##                                                222.1 
##                                               Zambia 
##                                                210.8 
##                                                Kenya 
##                                                168.7 
##                                              Liberia 
##                                                161.4 
##                                           Kazakhstan 
##                                                141.3 
##                                         Sierra Leone 
##                                                133.8 
##                                            Indonesia 
##                                                129.5 
##                                             Mongolia 
##                                                128.2 
##                          United Republic of Tanzania 
##                                                125.5 
##                                           Bangladesh 
##                                                124.4 
##                                  Republic of Moldova 
##                                                123.5 
##                                                Haiti 
##                                                123.2 
##                             Central African Republic 
##                                                121.9 
##                                                India 
##                                                103.2 
##                                          Philippines 
##                                                 98.4 
##                                              Georgia 
##                                                 96.9 
##                                                 Peru 
##                                                 96.3 
##                                              Somalia 
##                                                 95.1 
##                     Democratic Republic of the Congo 
##                                                 93.5 
##                                               Angola 
##                                                 93.1 
##                                           Kyrgyzstan 
##                                                 89.7 
##                                                 Chad 
##                                                 86.3 
##                                   Russian Federation 
##                                                 84.0 
##                                           Tajikistan 
##                                                 81.5 
##                                             Pakistan 
##                                                 72.5 
##                                           Uzbekistan 
##                                                 71.7 
##                                             Cameroon 
##                                                 71.4 
##                                              Ukraine 
##                                                 69.6 
##                                               Guinea 
##                                                 67.4 
##                                    Republic of Korea 
##                                                 66.5 
##                                              Myanmar 
##                                                 65.0 
##                                             Cambodia 
##                                                 64.2 
##                                                Congo 
##                                                 63.2 
##                                              Burundi 
##                                                 62.0 
##                                               Malawi 
##                                                 58.9 
##                                                China 
##                                                 54.9 
##                                 China, Hong Kong SAR 
##                                                 54.3 
##                                              Belarus 
##                                                 53.4 
##                                          Afghanistan 
##                                                 52.7 
##                                           Azerbaijan 
##                                                 52.6 
##                                           Madagascar 
##                                                 50.7 
##                                             Malaysia 
##                                                 49.9 
##                                              Nigeria 
##                                                 49.1 
##                     Lao People's Democratic Republic 
##                                                 48.5 
##                                              Romania 
##                                                 48.5 
##                                                Ghana 
##                                                 46.2 
##                                                 Iraq 
##                                                 45.4 
##                                                Sudan 
##                                                 45.4 
##                                             Ethiopia 
##                                                 45.2 
##                                              Morocco 
##                                                 44.8 
##                                               Rwanda 
##                                                 42.9 
##                                                Yemen 
##                                                 37.9 
##                                             Thailand 
##                                                 37.3 
##                                        Côte d'Ivoire 
##                                                 33.3 
##                     Bolivia (Plurinational State of) 
##                                                 33.2 
##                                              Ecuador 
##                                                 32.9 
##                                            Sri Lanka 
##                                                 29.3 
##                                                Niger 
##                                                 28.6 
##                                               Turkey 
##                                                 26.6 
##                                                 Togo 
##                                                 25.4 
##                                               Brazil 
##                                                 24.9 
##                                   Dominican Republic 
##                                                 24.9 
##                                             Paraguay 
##                                                 23.9 
##                                            Nicaragua 
##                                                 21.7 
##                                         Burkina Faso 
##                                                 19.2 
##                                             Honduras 
##                                                 18.5 
##                                                 Mali 
##                                                 18.1 
##                                                Nepal 
##                                                 17.0 
## United Kingdom of Great Britain and Northern Ireland 
##                                                 16.9 
##                                             Portugal 
##                                                 16.8 
##                                               Uganda 
##                                                 14.6 
##                                              Senegal 
##                                                 14.4 
##                                             Viet Nam 
##                                                 14.2 
##                                 Syrian Arab Republic 
##                                                 13.2 
##                                                Spain 
##                                                 12.9 
##                                               Poland 
##                                                 12.8 
##                                            Argentina 
##                                                 11.8 
##                                         Saudi Arabia 
##                                                 11.7 
##                                              Algeria 
##                                                 11.1 
##                                                Japan 
##                                                 10.9 
##                                            Guatemala 
##                                                 10.4 
##                   Venezuela (Bolivarian Republic of) 
##                                                 10.1 
##                           Iran (Islamic Republic of) 
##                                                  8.9 
##                                             Colombia 
##                                                  8.6 
##                                               Mexico 
##                                                  8.2 
##                                                Egypt 
##                                                  7.9 
##                                               France 
##                                                  7.4 
##                                                Chile 
##                                                  6.6 
##                                              Germany 
##                                                  5.9 
##                                                Italy 
##                                                  5.7 
##                                                Benin 
##                                                  4.7 
##                             United States of America 
##                                                  2.9 
##                                           Mozambique 
##                                                  2.5