rm(list=ls())
csdata <- read.csv("/Users/dasha/Desktop/NEW1.csv")
is.na(csdata)
## Country Year No_CSO_Increased. Rmtnc_Inflow_MIL GDP_Per_Capita
## [1,] FALSE FALSE FALSE FALSE FALSE
## [2,] FALSE FALSE FALSE FALSE FALSE
## [3,] FALSE FALSE FALSE FALSE FALSE
## [4,] FALSE FALSE FALSE FALSE FALSE
## [5,] FALSE FALSE FALSE FALSE FALSE
## [6,] FALSE FALSE FALSE FALSE FALSE
## [7,] FALSE FALSE FALSE FALSE FALSE
## [8,] FALSE FALSE FALSE FALSE FALSE
## [9,] FALSE FALSE FALSE FALSE FALSE
## [10,] FALSE FALSE FALSE FALSE FALSE
## [11,] FALSE FALSE FALSE FALSE FALSE
## [12,] FALSE FALSE FALSE FALSE FALSE
## [13,] FALSE FALSE FALSE FALSE FALSE
## [14,] FALSE FALSE FALSE FALSE FALSE
## [15,] FALSE FALSE FALSE FALSE FALSE
## [16,] FALSE FALSE FALSE FALSE FALSE
## [17,] FALSE FALSE FALSE FALSE FALSE
## [18,] FALSE FALSE FALSE FALSE FALSE
## [19,] FALSE FALSE FALSE FALSE FALSE
## [20,] FALSE FALSE FALSE FALSE FALSE
## [21,] FALSE FALSE FALSE FALSE FALSE
## [22,] FALSE FALSE FALSE FALSE FALSE
## [23,] FALSE FALSE FALSE FALSE FALSE
## [24,] FALSE FALSE FALSE FALSE FALSE
## [25,] FALSE FALSE FALSE FALSE FALSE
## [26,] FALSE FALSE FALSE FALSE FALSE
## [27,] FALSE FALSE FALSE FALSE FALSE
## [28,] FALSE FALSE FALSE FALSE FALSE
## [29,] FALSE FALSE FALSE FALSE FALSE
## [30,] FALSE FALSE FALSE FALSE FALSE
## [31,] FALSE FALSE FALSE FALSE FALSE
## [32,] FALSE FALSE FALSE FALSE FALSE
## [33,] FALSE FALSE FALSE FALSE FALSE
## [34,] FALSE FALSE FALSE FALSE FALSE
## [35,] FALSE FALSE FALSE FALSE FALSE
## [36,] FALSE FALSE FALSE FALSE FALSE
## [37,] FALSE FALSE FALSE FALSE FALSE
## [38,] FALSE FALSE FALSE FALSE FALSE
## [39,] FALSE FALSE FALSE FALSE FALSE
## [40,] FALSE FALSE FALSE FALSE FALSE
## [41,] FALSE FALSE FALSE FALSE FALSE
## [42,] FALSE FALSE FALSE FALSE FALSE
## [43,] FALSE FALSE FALSE FALSE FALSE
## [44,] FALSE FALSE FALSE FALSE FALSE
## [45,] FALSE FALSE FALSE FALSE FALSE
## [46,] FALSE FALSE FALSE FALSE FALSE
## [47,] FALSE FALSE FALSE FALSE FALSE
## [48,] FALSE FALSE FALSE FALSE FALSE
## [49,] FALSE FALSE FALSE FALSE FALSE
## [50,] FALSE FALSE FALSE FALSE FALSE
## [51,] FALSE FALSE FALSE FALSE FALSE
## [52,] FALSE FALSE FALSE FALSE FALSE
## [53,] FALSE FALSE FALSE FALSE FALSE
## [54,] FALSE FALSE FALSE FALSE FALSE
## [55,] FALSE FALSE FALSE FALSE FALSE
## [56,] FALSE FALSE FALSE FALSE FALSE
## [57,] FALSE FALSE FALSE FALSE FALSE
## [58,] FALSE FALSE FALSE FALSE FALSE
## [59,] FALSE FALSE FALSE FALSE FALSE
## [60,] FALSE FALSE FALSE FALSE FALSE
## [61,] FALSE FALSE FALSE FALSE FALSE
## [62,] FALSE FALSE FALSE FALSE FALSE
## [63,] FALSE FALSE FALSE FALSE FALSE
## [64,] FALSE FALSE FALSE FALSE FALSE
## [65,] FALSE FALSE FALSE FALSE FALSE
## [66,] FALSE FALSE FALSE FALSE FALSE
## [67,] FALSE FALSE FALSE FALSE FALSE
## [68,] FALSE FALSE FALSE FALSE FALSE
## [69,] FALSE FALSE FALSE FALSE FALSE
## [70,] FALSE FALSE FALSE FALSE FALSE
## [71,] FALSE FALSE FALSE FALSE FALSE
## [72,] FALSE FALSE FALSE FALSE FALSE
## [73,] FALSE FALSE FALSE FALSE FALSE
## [74,] FALSE FALSE FALSE FALSE FALSE
## [75,] FALSE FALSE FALSE FALSE FALSE
## [76,] FALSE FALSE FALSE FALSE FALSE
## [77,] FALSE FALSE FALSE FALSE FALSE
## [78,] FALSE FALSE FALSE FALSE FALSE
## [79,] FALSE FALSE FALSE FALSE FALSE
## [80,] FALSE FALSE FALSE FALSE FALSE
## [81,] FALSE FALSE FALSE FALSE FALSE
## [82,] FALSE FALSE FALSE FALSE FALSE
## [83,] FALSE FALSE FALSE FALSE FALSE
## [84,] FALSE FALSE FALSE FALSE FALSE
## [85,] FALSE FALSE FALSE FALSE FALSE
## [86,] FALSE FALSE FALSE FALSE FALSE
## [87,] FALSE FALSE FALSE FALSE FALSE
## [88,] FALSE FALSE FALSE FALSE FALSE
## [89,] FALSE FALSE FALSE FALSE FALSE
## [90,] FALSE FALSE FALSE FALSE FALSE
## [91,] FALSE FALSE FALSE FALSE FALSE
## [92,] FALSE FALSE FALSE FALSE FALSE
## [93,] FALSE FALSE FALSE FALSE FALSE
## [94,] FALSE FALSE FALSE FALSE FALSE
## [95,] FALSE FALSE FALSE FALSE FALSE
## [96,] FALSE FALSE FALSE FALSE FALSE
## [97,] FALSE FALSE FALSE FALSE FALSE
## [98,] FALSE FALSE FALSE FALSE FALSE
## [99,] FALSE FALSE FALSE FALSE FALSE
## [100,] FALSE FALSE FALSE FALSE FALSE
## [101,] FALSE FALSE FALSE FALSE FALSE
## [102,] FALSE FALSE FALSE FALSE FALSE
## [103,] FALSE FALSE FALSE FALSE FALSE
## [104,] FALSE FALSE FALSE FALSE FALSE
## [105,] FALSE FALSE FALSE FALSE FALSE
## [106,] FALSE FALSE FALSE FALSE FALSE
## [107,] FALSE FALSE FALSE FALSE FALSE
## [108,] FALSE FALSE FALSE FALSE FALSE
## [109,] FALSE FALSE FALSE FALSE FALSE
## [110,] FALSE FALSE FALSE FALSE FALSE
## [111,] FALSE FALSE FALSE FALSE FALSE
## [112,] FALSE FALSE FALSE FALSE FALSE
## [113,] FALSE FALSE FALSE FALSE FALSE
## [114,] FALSE FALSE FALSE FALSE FALSE
## [115,] FALSE FALSE FALSE FALSE FALSE
## [116,] FALSE FALSE FALSE FALSE FALSE
## [117,] FALSE FALSE FALSE FALSE FALSE
## [118,] FALSE FALSE FALSE FALSE FALSE
## [119,] FALSE FALSE FALSE FALSE FALSE
## [120,] FALSE FALSE FALSE FALSE FALSE
## [121,] FALSE FALSE FALSE FALSE FALSE
## [122,] FALSE FALSE FALSE FALSE FALSE
## [123,] FALSE FALSE FALSE FALSE FALSE
## [124,] FALSE FALSE FALSE FALSE FALSE
## [125,] FALSE FALSE FALSE FALSE FALSE
## [126,] FALSE FALSE FALSE FALSE FALSE
## [127,] FALSE FALSE FALSE FALSE FALSE
## [128,] FALSE FALSE FALSE FALSE FALSE
## [129,] FALSE FALSE FALSE FALSE FALSE
## [130,] FALSE FALSE FALSE FALSE FALSE
## [131,] FALSE FALSE FALSE FALSE FALSE
## [132,] FALSE FALSE FALSE FALSE FALSE
## [133,] FALSE FALSE FALSE FALSE FALSE
## [134,] FALSE FALSE FALSE FALSE FALSE
## [135,] FALSE FALSE FALSE FALSE FALSE
## [136,] FALSE FALSE FALSE FALSE FALSE
## [137,] FALSE FALSE FALSE FALSE FALSE
## [138,] FALSE FALSE FALSE FALSE FALSE
## [139,] FALSE FALSE FALSE FALSE FALSE
## [140,] FALSE FALSE FALSE FALSE FALSE
## [141,] FALSE FALSE FALSE FALSE FALSE
## [142,] FALSE FALSE FALSE FALSE FALSE
## [143,] FALSE FALSE FALSE FALSE FALSE
## [144,] FALSE FALSE FALSE FALSE FALSE
## [145,] FALSE FALSE FALSE FALSE FALSE
## [146,] FALSE FALSE FALSE FALSE FALSE
## [147,] FALSE FALSE FALSE FALSE FALSE
## [148,] FALSE FALSE FALSE FALSE FALSE
## [149,] FALSE FALSE FALSE FALSE FALSE
## [150,] FALSE FALSE FALSE FALSE FALSE
## [151,] FALSE FALSE FALSE FALSE FALSE
## [152,] FALSE FALSE FALSE FALSE FALSE
## [153,] FALSE FALSE FALSE FALSE FALSE
## [154,] FALSE FALSE FALSE FALSE FALSE
## [155,] FALSE FALSE FALSE FALSE FALSE
## [156,] FALSE FALSE FALSE FALSE FALSE
## [157,] FALSE FALSE FALSE FALSE FALSE
## [158,] FALSE FALSE FALSE FALSE FALSE
## [159,] FALSE FALSE FALSE FALSE FALSE
## [160,] FALSE FALSE FALSE FALSE FALSE
## [161,] FALSE FALSE FALSE FALSE FALSE
## [162,] FALSE FALSE FALSE FALSE FALSE
## [163,] FALSE FALSE FALSE FALSE FALSE
## [164,] FALSE FALSE FALSE FALSE FALSE
## [165,] FALSE FALSE FALSE FALSE FALSE
## [166,] FALSE FALSE FALSE FALSE FALSE
## [167,] FALSE FALSE FALSE FALSE FALSE
## [168,] FALSE FALSE FALSE FALSE FALSE
## [169,] FALSE FALSE FALSE FALSE FALSE
## [170,] FALSE FALSE FALSE FALSE FALSE
## [171,] FALSE FALSE FALSE FALSE FALSE
## [172,] FALSE FALSE FALSE FALSE FALSE
## [173,] FALSE FALSE FALSE FALSE FALSE
## [174,] FALSE FALSE FALSE FALSE FALSE
## [175,] FALSE FALSE FALSE FALSE FALSE
## [176,] FALSE FALSE FALSE FALSE FALSE
## [177,] FALSE FALSE FALSE FALSE FALSE
## [178,] FALSE FALSE FALSE FALSE FALSE
## [179,] FALSE FALSE FALSE FALSE FALSE
## [180,] FALSE FALSE FALSE FALSE FALSE
## [181,] FALSE FALSE FALSE FALSE FALSE
## [182,] FALSE FALSE FALSE FALSE FALSE
## [183,] FALSE FALSE FALSE FALSE FALSE
## [184,] FALSE FALSE FALSE FALSE FALSE
## [185,] FALSE FALSE FALSE FALSE FALSE
## [186,] FALSE FALSE FALSE FALSE FALSE
## [187,] FALSE FALSE FALSE FALSE FALSE
## [188,] FALSE FALSE FALSE FALSE FALSE
## [189,] FALSE FALSE FALSE FALSE FALSE
## [190,] FALSE FALSE FALSE FALSE FALSE
## [191,] FALSE FALSE FALSE FALSE FALSE
## [192,] FALSE FALSE FALSE FALSE FALSE
## [193,] FALSE FALSE FALSE FALSE FALSE
## [194,] FALSE FALSE FALSE FALSE FALSE
## [195,] FALSE FALSE FALSE FALSE FALSE
## [196,] FALSE FALSE FALSE FALSE FALSE
## [197,] FALSE FALSE FALSE FALSE FALSE
## [198,] FALSE FALSE FALSE FALSE FALSE
## [199,] FALSE FALSE FALSE FALSE FALSE
## [200,] FALSE FALSE FALSE FALSE FALSE
## Population Polity2
## [1,] FALSE TRUE
## [2,] FALSE FALSE
## [3,] FALSE FALSE
## [4,] FALSE FALSE
## [5,] FALSE FALSE
## [6,] FALSE FALSE
## [7,] FALSE FALSE
## [8,] FALSE FALSE
## [9,] FALSE TRUE
## [10,] FALSE TRUE
## [11,] FALSE FALSE
## [12,] FALSE FALSE
## [13,] FALSE FALSE
## [14,] FALSE FALSE
## [15,] FALSE FALSE
## [16,] FALSE FALSE
## [17,] FALSE TRUE
## [18,] FALSE TRUE
## [19,] FALSE FALSE
## [20,] FALSE FALSE
## [21,] FALSE FALSE
## [22,] FALSE FALSE
## [23,] FALSE FALSE
## [24,] FALSE FALSE
## [25,] FALSE TRUE
## [26,] FALSE TRUE
## [27,] FALSE FALSE
## [28,] FALSE FALSE
## [29,] FALSE FALSE
## [30,] FALSE FALSE
## [31,] FALSE FALSE
## [32,] FALSE FALSE
## [33,] FALSE FALSE
## [34,] FALSE FALSE
## [35,] FALSE FALSE
## [36,] FALSE FALSE
## [37,] FALSE FALSE
## [38,] FALSE FALSE
## [39,] FALSE FALSE
## [40,] FALSE FALSE
## [41,] FALSE FALSE
## [42,] FALSE FALSE
## [43,] FALSE FALSE
## [44,] FALSE FALSE
## [45,] FALSE FALSE
## [46,] FALSE FALSE
## [47,] FALSE FALSE
## [48,] FALSE FALSE
## [49,] FALSE FALSE
## [50,] FALSE FALSE
## [51,] FALSE FALSE
## [52,] FALSE FALSE
## [53,] FALSE FALSE
## [54,] FALSE FALSE
## [55,] FALSE FALSE
## [56,] FALSE FALSE
## [57,] FALSE TRUE
## [58,] FALSE TRUE
## [59,] FALSE FALSE
## [60,] FALSE FALSE
## [61,] FALSE FALSE
## [62,] FALSE FALSE
## [63,] FALSE FALSE
## [64,] FALSE FALSE
## [65,] FALSE FALSE
## [66,] FALSE FALSE
## [67,] FALSE FALSE
## [68,] FALSE FALSE
## [69,] FALSE TRUE
## [70,] FALSE TRUE
## [71,] FALSE FALSE
## [72,] FALSE FALSE
## [73,] FALSE FALSE
## [74,] FALSE FALSE
## [75,] FALSE FALSE
## [76,] FALSE FALSE
## [77,] FALSE FALSE
## [78,] FALSE FALSE
## [79,] FALSE FALSE
## [80,] FALSE FALSE
## [81,] FALSE FALSE
## [82,] FALSE FALSE
## [83,] FALSE TRUE
## [84,] FALSE FALSE
## [85,] FALSE FALSE
## [86,] FALSE FALSE
## [87,] FALSE FALSE
## [88,] FALSE FALSE
## [89,] FALSE FALSE
## [90,] FALSE FALSE
## [91,] FALSE TRUE
## [92,] FALSE TRUE
## [93,] FALSE FALSE
## [94,] FALSE FALSE
## [95,] FALSE FALSE
## [96,] FALSE FALSE
## [97,] FALSE FALSE
## [98,] FALSE FALSE
## [99,] FALSE FALSE
## [100,] FALSE FALSE
## [101,] FALSE FALSE
## [102,] FALSE FALSE
## [103,] FALSE FALSE
## [104,] FALSE FALSE
## [105,] FALSE FALSE
## [106,] FALSE FALSE
## [107,] FALSE TRUE
## [108,] FALSE TRUE
## [109,] FALSE FALSE
## [110,] FALSE FALSE
## [111,] FALSE FALSE
## [112,] FALSE FALSE
## [113,] FALSE FALSE
## [114,] FALSE FALSE
## [115,] FALSE FALSE
## [116,] FALSE FALSE
## [117,] FALSE FALSE
## [118,] FALSE FALSE
## [119,] FALSE FALSE
## [120,] FALSE FALSE
## [121,] FALSE FALSE
## [122,] FALSE FALSE
## [123,] FALSE FALSE
## [124,] FALSE FALSE
## [125,] FALSE FALSE
## [126,] FALSE FALSE
## [127,] FALSE FALSE
## [128,] FALSE FALSE
## [129,] FALSE FALSE
## [130,] FALSE FALSE
## [131,] FALSE FALSE
## [132,] FALSE FALSE
## [133,] FALSE FALSE
## [134,] FALSE FALSE
## [135,] FALSE FALSE
## [136,] FALSE FALSE
## [137,] FALSE FALSE
## [138,] FALSE FALSE
## [139,] FALSE FALSE
## [140,] FALSE FALSE
## [141,] FALSE FALSE
## [142,] FALSE FALSE
## [143,] FALSE FALSE
## [144,] FALSE FALSE
## [145,] FALSE FALSE
## [146,] FALSE FALSE
## [147,] FALSE FALSE
## [148,] FALSE FALSE
## [149,] FALSE TRUE
## [150,] FALSE TRUE
## [151,] FALSE FALSE
## [152,] FALSE FALSE
## [153,] FALSE FALSE
## [154,] FALSE FALSE
## [155,] FALSE FALSE
## [156,] FALSE FALSE
## [157,] FALSE FALSE
## [158,] FALSE FALSE
## [159,] FALSE FALSE
## [160,] FALSE FALSE
## [161,] FALSE FALSE
## [162,] FALSE FALSE
## [163,] FALSE FALSE
## [164,] FALSE FALSE
## [165,] FALSE FALSE
## [166,] FALSE FALSE
## [167,] FALSE FALSE
## [168,] FALSE FALSE
## [169,] FALSE FALSE
## [170,] FALSE FALSE
## [171,] FALSE FALSE
## [172,] FALSE FALSE
## [173,] FALSE FALSE
## [174,] FALSE FALSE
## [175,] FALSE FALSE
## [176,] FALSE FALSE
## [177,] FALSE FALSE
## [178,] FALSE FALSE
## [179,] FALSE FALSE
## [180,] FALSE FALSE
## [181,] FALSE FALSE
## [182,] FALSE FALSE
## [183,] FALSE FALSE
## [184,] FALSE FALSE
## [185,] FALSE FALSE
## [186,] FALSE FALSE
## [187,] FALSE FALSE
## [188,] FALSE FALSE
## [189,] FALSE TRUE
## [190,] FALSE TRUE
## [191,] FALSE FALSE
## [192,] FALSE FALSE
## [193,] FALSE FALSE
## [194,] FALSE FALSE
## [195,] FALSE FALSE
## [196,] FALSE FALSE
## [197,] FALSE FALSE
## [198,] FALSE FALSE
## [199,] FALSE FALSE
## [200,] FALSE FALSE
csdata$Polity2[csdata$v1==' '] <- NA
csdata1 <- na.omit(csdata)
names(csdata1)
## [1] "Country" "Year" "No_CSO_Increased."
## [4] "Rmtnc_Inflow_MIL" "GDP_Per_Capita" "Population"
## [7] "Polity2"
View(csdata1)
summary(csdata1)
## Country Year No_CSO_Increased. Rmtnc_Inflow_MIL
## Length:180 Min. :2005 Min. :0.0000 Min. : 0.00
## Class :character 1st Qu.:2005 1st Qu.:0.0000 1st Qu.: 65.68
## Mode :character Median :2015 Median :0.0000 Median : 500.74
## Mean :2010 Mean :0.4889 Mean : 3141.41
## 3rd Qu.:2015 3rd Qu.:1.0000 3rd Qu.: 2216.02
## Max. :2015 Max. :1.0000 Max. :68790.20
## GDP_Per_Capita Population Polity2
## Min. : 0 Min. :6.500e+01 Min. :-9.000e+00
## 1st Qu.: 811 1st Qu.:3.287e+06 1st Qu.:-3.000e+00
## Median : 1991 Median :1.107e+07 Median : 5.000e+00
## Mean : 5067340 Mean :5.691e+07 Mean : 1.332e+08
## 3rd Qu.: 4226 3rd Qu.:3.391e+07 3rd Qu.: 8.000e+00
## Max. :846741711 Max. :1.368e+09 Max. : 1.778e+10
cs <- csdata1[,c("No_CSO_Increased.", "Rmtnc_Inflow_MIL", "GDP_Per_Capita", "Population", "Polity2")]
pairs(cs,col=cs$No_CSO_Increased.,cex=.5)
# Fit a logistic regression model to predict "Increase in Number of CSOs" using Remittances, Democracy, GDP/Capita and Population
glm.fit=glm(No_CSO_Increased.~Rmtnc_Inflow_MIL+GDP_Per_Capita+Population+Polity2, data=csdata1,family=binomial)
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
summary(glm.fit)
##
## Call:
## glm(formula = No_CSO_Increased. ~ Rmtnc_Inflow_MIL + GDP_Per_Capita +
## Population + Polity2, family = binomial, data = csdata1)
##
## Deviance Residuals:
## Min 1Q Median 3Q Max
## -1.8376 -1.1099 -0.1582 1.1301 1.6850
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 1.278e-01 2.484e-01 0.514 0.60693
## Rmtnc_Inflow_MIL -1.079e-05 4.515e-05 -0.239 0.81116
## GDP_Per_Capita -1.737e-04 6.508e-05 -2.670 0.00760 **
## Population 1.112e-08 5.135e-09 2.165 0.03040 *
## Polity2 1.818e-06 6.947e-07 2.616 0.00889 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## (Dispersion parameter for binomial family taken to be 1)
##
## Null deviance: 249.44 on 179 degrees of freedom
## Residual deviance: 225.13 on 175 degrees of freedom
## AIC: 235.13
##
## Number of Fisher Scoring iterations: 16
# I use the predict function to predict the probability of an increase in the number of CSOs, given values of the predictors.
glm.probs=predict(glm.fit,type="response")
glm.probs[1:5]
## 2 3 4 5 6
## 0.5987587 0.4264919 0.3458949 0.4943204 0.4359150
# I coded "Increase in CSOs" as 1, so glm.probs are the probabilities of No_CSO_Increased. going up. Here I create actual labels instead of probabilities.
glm.pred=ifelse(glm.probs>0.5,"Increase","No Increase")
# Confusion matrix
attach(csdata1)
table(glm.pred,No_CSO_Increased.) # predictions vs. truth
## No_CSO_Increased.
## glm.pred 0 1
## Increase 37 54
## No Increase 55 34
mean(glm.pred==No_CSO_Increased.)
## [1] 0
# Create a vector corresponding to the observations from 2005
train = Year<2015
# Fit the logistic regression using only the train data
glm.fit=glm(No_CSO_Increased.~Rmtnc_Inflow_MIL+GDP_Per_Capita+Population+Polity2, data=csdata1,family=binomial, subset=train)
# Test the model by fitting the test data into the fitted model
glm.probs=predict(glm.fit,newdata=csdata1[!train,],type="response")
# Compute the predictions for 2015 and compare them to the actual
# increase in number of CSOs over that time period
glm.pred=ifelse(glm.probs >0.5,"1","0")
No_CSO_Increased..2015=csdata1$No_CSO_Increased[!train]
table(glm.pred,No_CSO_Increased..2015)
## No_CSO_Increased..2015
## glm.pred 0 1
## 0 20 7
## 1 44 20
mean(glm.pred==No_CSO_Increased..2015)
## [1] 0.4395604
mean(glm.pred != No_CSO_Increased..2015)
## [1] 0.5604396
20/(44+20)
## [1] 0.3125
plot(Rmtnc_Inflow_MIL)