if neccessary install: install.packages (“doBy”) ;install.packages (“ggplot2”)

rm(list = ls(all = TRUE))
library(doBy)
library(ggplot2)
setwd("~/Documents/tesisGMU/survival")
file = "WankaLab-table.csv"
data = read.csv(file, header = T, skip = 6)
names(data)
##  [1] "X.run.number."               "RuralShare_PopulationGrowth"
##  [3] "seeScarcityLevel."           "thresholdToMigrate"         
##  [5] "systems"                     "glacier_effect"             
##  [7] "populationGrowth"            "rainSeasonReserve"          
##  [9] "drySeasonShare"              "LimitUrbanArea"             
## [11] "prop_INmigrants"             "Water_Loss"                 
## [13] "X.step."                     "moment_conflict"            
## [15] "moment_migrate"
data = data[, c(1, 4, 6, 13, 14, 15)]
names(data)[1] = c("SimulationNumber")
names(data)[4] = c("SimulationDuration")
names(data)[5] = c("TimeUntilConflict")
names(data)[6] = c("TimeUntilMigration")

data <- data[order(data$SimulationNumber, data$SimulationDuration), ]


dataMig = data
rownames(dataMig) = NULL
dataMig$censoredMig = NA

dataConf = data
rownames(dataConf) = NULL
dataConf$censoredConf = NA

dataMig$TimeUntilMigration[dataMig$TimeUntilMigration == -1] = NA
dataMig = as.data.frame(summaryBy(TimeUntilMigration ~ SimulationNumber + thresholdToMigrate + 
    glacier_effect, data = dataMig, FUN = function(x) {
    min = min(x, na.rm = T)
}))
names(dataMig)[4] = c("TimeUntilMigration")

dataConf$TimeUntilConflict[dataConf$TimeUntilConflict == -1] = NA
dataConf = as.data.frame(summaryBy(TimeUntilConflict ~ SimulationNumber + thresholdToMigrate + 
    glacier_effect, data = dataConf, FUN = function(x) {
    min = min(x, na.rm = T)
}))
names(dataConf)[4] = c("TimeUntilConflict")
dataConf$TimeUntilConflict[dataConf$TimeUntilConflict == Inf] = 120

dataConf$status = NA
dataConf$status[dataConf$TimeUntilConflict == 120] = 0
dataConf$status[dataConf$TimeUntilConflict < 120] = 1
summary(dataConf)
##  SimulationNumber thresholdToMigrate glacier_effect TimeUntilConflict
##  Min.   :  1      Min.   :2          Min.   :0.10   Min.   : 98      
##  1st Qu.:226      1st Qu.:2          1st Qu.:0.10   1st Qu.:115      
##  Median :450      Median :3          Median :0.15   Median :120      
##  Mean   :450      Mean   :3          Mean   :0.15   Mean   :117      
##  3rd Qu.:675      3rd Qu.:4          3rd Qu.:0.20   3rd Qu.:120      
##  Max.   :900      Max.   :4          Max.   :0.20   Max.   :120      
##      status     
##  Min.   :0.000  
##  1st Qu.:0.000  
##  Median :0.000  
##  Mean   :0.476  
##  3rd Qu.:1.000  
##  Max.   :1.000
summaryBy(TimeUntilConflict ~ glacier_effect, data = dataConf, FUN = function(x) {
    c(mean = mean(x), sd = sd(x), me = median(x))
})
##   glacier_effect TimeUntilConflict.mean TimeUntilConflict.sd
## 1           0.10                  117.7                3.857
## 2           0.15                  117.4                3.938
## 3           0.20                  116.0                4.912
##   TimeUntilConflict.me
## 1                  120
## 2                  120
## 3                  118
DissimilarityBox <- ggplot(dataConf, aes(factor(glacier_effect), TimeUntilConflict))
DissimilarityBox + geom_boxplot(fill = "white", colour = "#3366FF") + xlab("Reduction in water balance as glacier retreats") + 
    ylab("Simulation Duration") + ggtitle("Effect of Glacier on Time Until Conflict")

plot of chunk unnamed-chunk-4

dataMig$status = NA
dataMig$status[dataMig$TimeUntilMigration == 120] = 0
dataMig$status[dataMig$TimeUntilMigration < 120] = 1
summary(dataMig)
##  SimulationNumber thresholdToMigrate glacier_effect TimeUntilMigration
##  Min.   :  1      Min.   :2          Min.   :0.10   Min.   : 92       
##  1st Qu.:226      1st Qu.:2          1st Qu.:0.10   1st Qu.: 99       
##  Median :450      Median :3          Median :0.15   Median :102       
##  Mean   :450      Mean   :3          Mean   :0.15   Mean   :102       
##  3rd Qu.:675      3rd Qu.:4          3rd Qu.:0.20   3rd Qu.:105       
##  Max.   :900      Max.   :4          Max.   :0.20   Max.   :117       
##      status 
##  Min.   :1  
##  1st Qu.:1  
##  Median :1  
##  Mean   :1  
##  3rd Qu.:1  
##  Max.   :1
summaryBy(TimeUntilMigration ~ glacier_effect, data = dataMig, FUN = function(x) {
    c(mean = mean(x), sd = sd(x), me = median(x))
})
##   glacier_effect TimeUntilMigration.mean TimeUntilMigration.sd
## 1           0.10                   103.4                 4.025
## 2           0.15                   102.1                 3.930
## 3           0.20                   101.1                 4.228
##   TimeUntilMigration.me
## 1                   103
## 2                   102
## 3                   100
DissimilarityBox <- ggplot(dataConf, aes(factor(thresholdToMigrate), TimeUntilConflict))
DissimilarityBox + geom_boxplot(fill = "white", colour = "#3366FF") + xlab("Max time (years) a person waits to migrate ") + 
    ylab("Simulation Duration") + ggtitle("Effect of Threshold To Migrate on Time Until Conflict")

plot of chunk unnamed-chunk-7

Survival

survobj <- with(dataMig, Surv(TimeUntilMigration, status))
summary(survobj)
##       time         status 
##  Min.   : 92   Min.   :1  
##  1st Qu.: 99   1st Qu.:1  
##  Median :102   Median :1  
##  Mean   :102   Mean   :1  
##  3rd Qu.:105   3rd Qu.:1  
##  Max.   :117   Max.   :1

Plot survival distribution of the total sample

Kaplan-Meier estimator

fit0 <- survfit(survobj ~ 1, data = dataMig)
# summary(fit0)
plot(fit0, xlab = "Survival Time in 6-month periods", ylab = "% Simulations without Migration", 
    yscale = 100, main = "Survival Distribution (Overall)")

plot of chunk unnamed-chunk-9

Compare the survival distributions for thresholdToMigrate

fit1 <- survfit(survobj ~ as.factor(thresholdToMigrate), data = dataMig)
# summary(dataMig$thresholdToMigrate) plot the survival distributions by sex
plot(fit1, xlab = "Survival Time in 6-month periods", ylab = "% Simulations without Migration", 
    yscale = 100, col = c("red", "blue", "green"), main = "Survival Distributions by thresholdToMigrate")
legend("bottomleft", title = "thresholdToMigrate", c("2", "3", "4"), fill = c("red", 
    "blue", "green"))

plot of chunk unnamed-chunk-10

Compare the survival distributions for Glacier effect

fit1 <- survfit(survobj ~ as.factor(glacier_effect), data = dataMig)
# summary(dataMig$thresholdToMigrate) plot the survival distributions by sex
plot(fit1, xlab = "Survival Time in 6-month periods", ylab = "% Simulations without Migration", 
    yscale = 100, col = c("red", "blue", "green"), main = "Survival Distributions by thresholdToMigrate")
legend("bottomleft", title = "Glacier Effect", c("0.1", "0.15", "0.2"), fill = c("red", 
    "blue", "green"))

plot of chunk unnamed-chunk-11

CONFLICT

survobj <- with(dataConf, Surv(TimeUntilConflict, status))
summary(survobj)
##       time         status     
##  Min.   : 98   Min.   :0.000  
##  1st Qu.:115   1st Qu.:0.000  
##  Median :120   Median :0.000  
##  Mean   :117   Mean   :0.476  
##  3rd Qu.:120   3rd Qu.:1.000  
##  Max.   :120   Max.   :1.000

Plot survival distribution of the total sample

Kaplan-Meier estimator

fit0 <- survfit(survobj ~ 1, data = dataConf)
# summary(fit0)
plot(fit0, xlab = "Survival Time in 6-month periods", ylab = "% Simulations without Conflict", 
    yscale = 100, main = "Survival Distribution (Overall)")

plot of chunk unnamed-chunk-13

Compare the survival distributions for thresholdToMigrate

fit1 <- survfit(survobj ~ as.factor(thresholdToMigrate), data = dataConf)
# summary(dataMig$thresholdToMigrate) plot the survival distributions by sex
plot(fit1, xlab = "Survival Time in 6-month periods", ylab = "% Simulations without Conflict", 
    yscale = 100, col = c("red", "blue", "green"), main = "Survival Distributions by thresholdToMigrate")
legend("bottomleft", title = "thresholdToMigrate", c("2", "3", "4"), fill = c("red", 
    "blue", "green"))

plot of chunk unnamed-chunk-14

Compare the survival distributions for Glacier effect

fit1 <- survfit(survobj ~ as.factor(glacier_effect), data = dataConf)
# summary(dataMig$thresholdToMigrate) plot the survival distributions by sex
plot(fit1, xlab = "Survival Time in 6-month periods", ylab = "% Simulations without Conflict", 
    yscale = 100, col = c("red", "blue", "green"), main = "Survival Distributions by thresholdToMigrate")
legend("bottomleft", title = "Glacier Effect", c("0.1", "0.15", "0.2"), fill = c("red", 
    "blue", "green"))

plot of chunk unnamed-chunk-15