# load the ncdf4 and ggplot2 packages

library(ncdf4)
library(ggplot2)
library(reticulate)
library(grid)
library(gridExtra)

load("NorthAfrica_em.RData")
load("NorthAfrica_od.RData")
load("NorthAfrica_ustar.RData")

# # #_______________________________Annual_____________________________________________________________________
vectorod <- c((as.vector(NorthAfrica_od[seq(4,2924,by=8),])))
vectorem <- c((as.vector(NorthAfrica_em[seq(4,2924,by=8),])))
vectorod <- vectorod*0.31
vectorem <- vectorem*0.31

od_steps <- c(0, 1:9 %o% 10^(-(4:0)))
od_steps <- c(od_steps,c(1:9 %o% 10^((1))))
em_steps <- c(0, 1:9 %o% 10^(-(11:-2)))

slope <- matrix(0,length(em_steps),length(od_steps))
intercept <- matrix(0,length(em_steps),length(od_steps))
R2 <- matrix(0,length(em_steps),length(od_steps))
r <- matrix(0,length(em_steps),length(od_steps))
RMSE <- matrix(0,length(em_steps),length(od_steps))
stderrint <- matrix(0,length(em_steps),length(od_steps))
stderrslp <- matrix(0,length(em_steps),length(od_steps))

for (d in 1:length(od_steps))
{
  print("od_steps=")
  print(od_steps[d])
  for (em in 1:length(em_steps))
  {
    NorthAfrica_od0<- vectorod[(vectorem>=em_steps[em])&(vectorod>=od_steps[d])]
    NorthAfrica_em0<- vectorem[(vectorem>=em_steps[em])&(vectorod>=od_steps[d])]
    if (length(NorthAfrica_od0)<2)
    {
      r[em,d] <- -999
      R2[em,d] <- -999
      intercept[em,d] <- -999
      slope[em,d] <- -999
      stderrint[em,d] <- -999
      stderrslp[em,d] <- -999
      RMSE[em,d]  <- -999
    }
    else
    {
      r[em,d] <- cor(NorthAfrica_em0, NorthAfrica_od0)
      linearMod <- lm(NorthAfrica_od0 ~ NorthAfrica_em0)  # build linear regression model on full data
      RSS <- c(crossprod(linearMod$residuals))
      MSE <- RSS / length(linearMod$residuals)
      rmsex <- sqrt(MSE)
      RMSE[em,d] <- rmsex
      R2[em,d] <- summary(linearMod)$r.squared
      intercept[em,d] <- summary(linearMod)$coefficients[1,1]
      if (all(NorthAfrica_em0==rep(0,length(NorthAfrica_em0))))
      {
        slope[em,d] <- -999
        stderrslp[em,d] <- -999
      }
      else{
        slope[em,d] <- summary(linearMod)$coefficients[2,1]
        stderrslp[em,d] <- summary(linearMod)$coefficients[2,2]
      }
      stderrint[em,d] <- summary(linearMod)$coefficients[1,2]
    }
  }
}
R2_em <- R2
slope_em <- slope
save(R2_em, file = "R2_em.RData")
save(slope_em, file = "slope_em.RData")
RMSE_em <- RMSE
save(RMSE_em, file = "RMSE_em.RData")
intercept_em <- intercept
save(intercept_em, file = "intercept_em.RData")

#_______________________________DJF_____________________________________________________________________
vectorod <- c((as.vector(NorthAfrica_od[seq(4,476,by=8),])))
vectorem <- c((as.vector(NorthAfrica_em[seq(4,476,by=8),])))
vectorod1 <- c((as.vector(NorthAfrica_od[seq(2684,2924,by=8),])))
vectorem1 <- c((as.vector(NorthAfrica_em[seq(2684,2924,by=8),])))
vectorod <- c(vectorod1,vectorod)
vectorem <- c(vectorem1,vectorem)
vectorod <- vectorod*0.31
vectorem <- vectorem*0.31

od_steps <- c(0, 1:9 %o% 10^(-(4:0)))
od_steps <- c(od_steps,c(1:9 %o% 10^((1))))
em_steps <- c(0, 1:9 %o% 10^(-(11:-2)))

slope <- matrix(0,length(em_steps),length(od_steps))
intercept <- matrix(0,length(em_steps),length(od_steps))
R2 <- matrix(0,length(em_steps),length(od_steps))
r <- matrix(0,length(em_steps),length(od_steps))
stderrint <- matrix(0,length(em_steps),length(od_steps))
stderrslp <- matrix(0,length(em_steps),length(od_steps))
RMSE <- matrix(0,length(em_steps),length(od_steps))

for (d in 1:length(od_steps))
{
  print("od_steps=")
  print(od_steps[d])
  for (em in 1:length(em_steps))
  {
    NorthAfrica_od0<- vectorod[(vectorem>=em_steps[em])&(vectorod>=od_steps[d])]
    NorthAfrica_em0<- vectorem[(vectorem>=em_steps[em])&(vectorod>=od_steps[d])]
    if (length(NorthAfrica_od0)<2)
    {
      r[em,d] <- -999
      R2[em,d] <- -999
      intercept[em,d] <- -999
      slope[em,d] <- -999
      stderrint[em,d] <- -999
      stderrslp[em,d] <- -999
      RMSE[em,d]  <- -999
    }
    else
    {
      r[em,d] <- cor(NorthAfrica_em0, NorthAfrica_od0)
      linearMod <- lm(NorthAfrica_od0 ~ NorthAfrica_em0)  # build linear regression model on full data
      RSS <- c(crossprod(linearMod$residuals))
      MSE <- RSS / length(linearMod$residuals)
      rmsex <- sqrt(MSE)
      RMSE[em,d] <- rmsex
      R2[em,d] <- summary(linearMod)$r.squared
      intercept[em,d] <- summary(linearMod)$coefficients[1,1]
      if (all(NorthAfrica_em0==rep(0,length(NorthAfrica_em0))))
      {
        slope[em,d] <- -999
        stderrslp[em,d] <- -999
      }
      else{
        slope[em,d] <- summary(linearMod)$coefficients[2,1]
        stderrslp[em,d] <- summary(linearMod)$coefficients[2,2]
      }
      stderrint[em,d] <- summary(linearMod)$coefficients[1,2]
    }
    
  }
}

R2_em_DJF <- R2
stderrint_em_DJF <- stderrint
stderrslp_em_DJF <- stderrslp
slope_em_DJF <- slope
intercept_em_DJF <- intercept
save(intercept_em_DJF, file = "intercept_em_DJF.RData")
save(R2_em_DJF, file = "R2_em_DJF.RData")
save(slope_em_DJF, file = "slope_em_DJF.RData")
save(stderrint_em_DJF, file = "stderrint_em_DJF.RData")
save(stderrslp_em_DJF, file = "stderrslp_em_DJF.RData")
RMSE_em_DJF <- RMSE
save(RMSE_em_DJF, file = "RMSE_em_DJF.RData")

#_______________________________MAM_____________________________________________________________________
vectorod <- c((as.vector(NorthAfrica_od[seq(484,1212,by=8),])))
vectorem <- c((as.vector(NorthAfrica_em[seq(484,1212,by=8),])))
vectorod <- vectorod*0.31
vectorem <- vectorem*0.31

od_steps <- c(0, 1:9 %o% 10^(-(4:0)))
od_steps <- c(od_steps,c(1:9 %o% 10^((1))))
em_steps <- c(0, 1:9 %o% 10^(-(11:-2)))

slope <- matrix(0,length(em_steps),length(od_steps))
intercept <- matrix(0,length(em_steps),length(od_steps))
R2 <- matrix(0,length(em_steps),length(od_steps))
r <- matrix(0,length(em_steps),length(od_steps))
stderrint <- matrix(0,length(em_steps),length(od_steps))
stderrslp <- matrix(0,length(em_steps),length(od_steps))
RMSE <- matrix(0,length(em_steps),length(od_steps))

for (d in 1:length(od_steps))
{
  print("od_steps=")
  print(od_steps[d])
  for (em in 1:length(em_steps))
  {
    NorthAfrica_od0<- vectorod[(vectorem>=em_steps[em])&(vectorod>=od_steps[d])]
    NorthAfrica_em0<- vectorem[(vectorem>=em_steps[em])&(vectorod>=od_steps[d])]
    if (length(NorthAfrica_od0)<2)
    {
      r[em,d] <- -999
      R2[em,d] <- -999
      intercept[em,d] <- -999
      slope[em,d] <- -999
      stderrint[em,d] <- -999
      stderrslp[em,d] <- -999
      RMSE[em,d]  <- -999
    }
    else
    {
      r[em,d] <- cor(NorthAfrica_em0, NorthAfrica_od0)
      linearMod <- lm(NorthAfrica_od0 ~ NorthAfrica_em0)  # build linear regression model on full data
      RSS <- c(crossprod(linearMod$residuals))
      MSE <- RSS / length(linearMod$residuals)
      rmsex <- sqrt(MSE)
      RMSE[em,d] <- rmsex
      R2[em,d] <- summary(linearMod)$r.squared
      intercept[em,d] <- summary(linearMod)$coefficients[1,1]
      if (all(NorthAfrica_em0==rep(0,length(NorthAfrica_em0))))
      {
        slope[em,d] <- -999
        stderrslp[em,d] <- -999
      }
      else{
        slope[em,d] <- summary(linearMod)$coefficients[2,1]
        stderrslp[em,d] <- summary(linearMod)$coefficients[2,2]
      }
      stderrint[em,d] <- summary(linearMod)$coefficients[1,2]
    }
    
  }
}

R2_em_MAM <- R2
stderrint_em_MAM <- R2
stderrslp_em_MAM <- R2
slope_em_MAM <- slope
intercept_em_MAM <- intercept
save(slope_em_MAM, file = "slope_em_MAM.RData")
save(intercept_em_MAM, file = "intercept_em_MAM.RData")
save(R2_em_MAM, file = "R2_em_MAM.RData")
save(stderrint_em_MAM, file = "stderrint_em_MAM.RData")
save(stderrslp_em_MAM, file = "stderrslp_em_MAM.RData")
RMSE_em_MAM <- RMSE
save(RMSE_em_MAM, file = "RMSE_em_MAM.RData")

# #_______________________________JJA_____________________________________________________________________

vectorod <- c((as.vector(NorthAfrica_od[seq(1220,1948,by=8),])))
vectorem <- c((as.vector(NorthAfrica_em[seq(1220,1948,by=8),])))
vectorod <- vectorod*0.31
vectorem <- vectorem*0.31

od_steps <- c(0, 1:9 %o% 10^(-(4:0)))
od_steps <- c(od_steps,c(1:9 %o% 10^((1))))
em_steps <- c(0, 1:9 %o% 10^(-(11:-2)))

slope <- matrix(0,length(em_steps),length(od_steps))
intercept <- matrix(0,length(em_steps),length(od_steps))
R2 <- matrix(0,length(em_steps),length(od_steps))
r <- matrix(0,length(em_steps),length(od_steps))
stderrint <- matrix(0,length(em_steps),length(od_steps))
stderrslp <- matrix(0,length(em_steps),length(od_steps))
RMSE <- matrix(0,length(em_steps),length(od_steps))

for (d in 1:length(od_steps))
{
  print("od_steps=")
  print(od_steps[d])
  for (em in 1:length(em_steps))
  {
    NorthAfrica_od0<- vectorod[(vectorem>=em_steps[em])&(vectorod>=od_steps[d])]
    NorthAfrica_em0<- vectorem[(vectorem>=em_steps[em])&(vectorod>=od_steps[d])]
    if (length(NorthAfrica_od0)<2)
    {
      r[em,d] <- -999
      R2[em,d] <- -999
      intercept[em,d] <- -999
      slope[em,d] <- -999
      stderrint[em,d] <- -999
      stderrslp[em,d] <- -999
      RMSE[em,d]  <- -999
    }
    else
    {
      r[em,d] <- cor(NorthAfrica_em0, NorthAfrica_od0)
      linearMod <- lm(NorthAfrica_od0 ~ NorthAfrica_em0)  # build linear regression model on full data
      RSS <- c(crossprod(linearMod$residuals))
      MSE <- RSS / length(linearMod$residuals)
      rmsex <- sqrt(MSE)
      RMSE[em,d] <- rmsex
      R2[em,d] <- summary(linearMod)$r.squared
      intercept[em,d] <- summary(linearMod)$coefficients[1,1]
      if (all(NorthAfrica_em0==rep(0,length(NorthAfrica_em0))))
      {
        slope[em,d] <- -999
        stderrslp[em,d] <- -999
      }
      else{
        slope[em,d] <- summary(linearMod)$coefficients[2,1]
        stderrslp[em,d] <- summary(linearMod)$coefficients[2,2]
      }
      stderrint[em,d] <- summary(linearMod)$coefficients[1,2]
    }
  }
}

R2_em_JJA <- R2
stderrint_em_JJA <- R2
stderrslp_em_JJA <- R2
slope_em_JJA <- slope
intercept_em_JJA <- intercept
save(slope_em_JJA, file = "slope_em_JJA.RData")
save(intercept_em_JJA, file = "intercept_em_JJA.RData")
save(R2_em_JJA, file = "R2_em_JJA.RData")
save(stderrint_em_JJA, file = "stderrint_em_JJA.RData")
save(stderrslp_em_JJA, file = "stderrslp_em_JJA.RData")
RMSE_em_JJA <- RMSE
save(RMSE_em_JJA, file = "RMSE_em_JJA.RData")

# #_______________________________SON_____________________________________________________________________

vectorod <- c((as.vector(NorthAfrica_od[seq(1956,2676,by=8),])))
vectorem <- c((as.vector(NorthAfrica_em[seq(1956,2676,by=8),])))
vectorod <- vectorod*0.31
vectorem <- vectorem*0.31

od_steps <- c(0, 1:9 %o% 10^(-(4:0)))
od_steps <- c(od_steps,c(1:9 %o% 10^((1))))
em_steps <- c(0, 1:9 %o% 10^(-(11:-2)))

slope <- matrix(0,length(em_steps),length(od_steps))
intercept <- matrix(0,length(em_steps),length(od_steps))
R2 <- matrix(0,length(em_steps),length(od_steps))
r <- matrix(0,length(em_steps),length(od_steps))
stderrint <- matrix(0,length(em_steps),length(od_steps))
stderrslp <- matrix(0,length(em_steps),length(od_steps))
RMSE <- matrix(0,length(em_steps),length(od_steps))

for (d in 1:length(od_steps))
{
  print("od_steps=")
  print(od_steps[d])
  for (em in 1:length(em_steps))
  {
    NorthAfrica_od0<- vectorod[(vectorem>=em_steps[em])&(vectorod>=od_steps[d])]
    NorthAfrica_em0<- vectorem[(vectorem>=em_steps[em])&(vectorod>=od_steps[d])]
    if (length(NorthAfrica_od0)<2)
    {
      r[em,d] <- -999
      R2[em,d] <- -999
      intercept[em,d] <- -999
      slope[em,d] <- -999
      stderrint[em,d] <- -999
      stderrslp[em,d] <- -999
      RMSE[em,d]  <- -999
    }
    else
    {
      r[em,d] <- cor(NorthAfrica_em0, NorthAfrica_od0)
      linearMod <- lm(NorthAfrica_od0 ~ NorthAfrica_em0)  # build linear regression model on full data
      RSS <- c(crossprod(linearMod$residuals))
      MSE <- RSS / length(linearMod$residuals)
      rmsex <- sqrt(MSE)
      RMSE[em,d] <- rmsex
      R2[em,d] <- summary(linearMod)$r.squared
      intercept[em,d] <- summary(linearMod)$coefficients[1,1]
      if (all(NorthAfrica_em0==rep(0,length(NorthAfrica_em0))))
      {
        slope[em,d] <- -999
        stderrslp[em,d] <- -999
      }
      else{
        slope[em,d] <- summary(linearMod)$coefficients[2,1]
        stderrslp[em,d] <- summary(linearMod)$coefficients[2,2]
      }
      stderrint[em,d] <- summary(linearMod)$coefficients[1,2]
    }
  }
}

R2_em_SON <- R2
stderrint_em_SON <- R2
stderrslp_em_SON <- R2
slope_em_SON <- slope
intercept_em_SON <- intercept
save(slope_em_SON, file = "slope_em_SON.RData")
save(intercept_em_SON, file = "intercept_em_SON.RData")
save(R2_em_SON, file = "R2_em_SON.RData")
save(stderrint_em_SON, file = "stderrint_em_SON.RData")
save(stderrslp_em_SON, file = "stderrslp_em_SON.RData")
RMSE_em_SON <- RMSE
save(RMSE_em_SON, file = "RMSE_em_SON.RData")

#---------------------------------January---------------------
vectorod <- c((as.vector(NorthAfrica_od[seq(4,244,by=8),])))
vectorem <- c((as.vector(NorthAfrica_em[seq(4,244,by=8),])))
vectorod <- vectorod*0.31
vectorem <- vectorem*0.31

slope <- matrix(0,length(em_steps),length(od_steps))
intercept <- matrix(0,length(em_steps),length(od_steps))
R2 <- matrix(0,length(em_steps),length(od_steps))
r <- matrix(0,length(em_steps),length(od_steps))
stderrint <- matrix(0,length(em_steps),length(od_steps))
stderrslp <- matrix(0,length(em_steps),length(od_steps))
RMSE <- matrix(0,length(em_steps),length(od_steps))

for (d in 1:length(od_steps))
{
  print("od_steps=")
  print(od_steps[d])
  for (em in 1:length(em_steps))
  {
    NorthAfrica_od0<- vectorod[(vectorem>=em_steps[em])&(vectorod>=od_steps[d])]
    NorthAfrica_em0<- vectorem[(vectorem>=em_steps[em])&(vectorod>=od_steps[d])]
    if (length(NorthAfrica_od0)<2)
    {
      r[em,d] <- -999
      R2[em,d] <- -999
      intercept[em,d] <- -999
      slope[em,d] <- -999
      stderrint[em,d] <- -999
      stderrslp[em,d] <- -999
      RMSE[em,d]  <- -999
    }
    else
    {
      r[em,d] <- cor(NorthAfrica_em0, NorthAfrica_od0)
      linearMod <- lm(NorthAfrica_od0 ~ NorthAfrica_em0)  # build linear regression model on full data
      RSS <- c(crossprod(linearMod$residuals))
      MSE <- RSS / length(linearMod$residuals)
      rmsex <- sqrt(MSE)
      RMSE[em,d] <- rmsex
      R2[em,d] <- summary(linearMod)$r.squared
      intercept[em,d] <- summary(linearMod)$coefficients[1,1]
      if (all(NorthAfrica_em0==rep(0,length(NorthAfrica_em0))))
      {
        slope[em,d] <- -999
        stderrslp[em,d] <- -999
      }
      else{
        slope[em,d] <- summary(linearMod)$coefficients[2,1]
        stderrslp[em,d] <- summary(linearMod)$coefficients[2,2]
      }
      stderrint[em,d] <- summary(linearMod)$coefficients[1,2]
    }
  }
}

R2_em_January <- R2
stderrint_em_January <- R2
stderrslp_em_January <- R2
slope_em_January <- slope
intercept_em_January <- intercept
save(slope_em_January, file = "slope_em_January.RData")
save(intercept_em_January, file = "intercept_em_January.RData")
save(R2_em_January, file = "R2_em_January.RData")
save(stderrint_em_January, file = "stderrint_em_January.RData")
save(stderrslp_em_January, file = "stderrslp_em_January.RData")
RMSE_em_January <- RMSE
save(RMSE_em_January, file = "RMSE_em_January.RData")


#---------------------------------February---------------------
vectorod <- c((as.vector(NorthAfrica_od[seq(252,476,by=8),])))
vectorem <- c((as.vector(NorthAfrica_em[seq(252,476,by=8),])))
vectorod <- vectorod*0.31
vectorem <- vectorem*0.31

slope <- matrix(0,length(em_steps),length(od_steps))
intercept <- matrix(0,length(em_steps),length(od_steps))
R2 <- matrix(0,length(em_steps),length(od_steps))
r <- matrix(0,length(em_steps),length(od_steps))
stderrint <- matrix(0,length(em_steps),length(od_steps))
stderrslp <- matrix(0,length(em_steps),length(od_steps))
RMSE <- matrix(0,length(em_steps),length(od_steps))

for (d in 1:length(od_steps))
{
  print("od_steps=")
  print(od_steps[d])
  for (em in 1:length(em_steps))
  {
    NorthAfrica_od0<- vectorod[(vectorem>=em_steps[em])&(vectorod>=od_steps[d])]
    NorthAfrica_em0<- vectorem[(vectorem>=em_steps[em])&(vectorod>=od_steps[d])]
    if (length(NorthAfrica_od0)<2)
    {
      r[em,d] <- -999
      R2[em,d] <- -999
      intercept[em,d] <- -999
      slope[em,d] <- -999
      stderrint[em,d] <- -999
      stderrslp[em,d] <- -999
      RMSE[em,d]  <- -999
    }
    else
    {
      r[em,d] <- cor(NorthAfrica_em0, NorthAfrica_od0)
      linearMod <- lm(NorthAfrica_od0 ~ NorthAfrica_em0)  # build linear regression model on full data
      RSS <- c(crossprod(linearMod$residuals))
      MSE <- RSS / length(linearMod$residuals)
      rmsex <- sqrt(MSE)
      RMSE[em,d] <- rmsex
      R2[em,d] <- summary(linearMod)$r.squared
      intercept[em,d] <- summary(linearMod)$coefficients[1,1]
      if (all(NorthAfrica_em0==rep(0,length(NorthAfrica_em0))))
      {
        slope[em,d] <- -999
        stderrslp[em,d] <- -999
      }
      else{
        slope[em,d] <- summary(linearMod)$coefficients[2,1]
        stderrslp[em,d] <- summary(linearMod)$coefficients[2,2]
      }
      stderrint[em,d] <- summary(linearMod)$coefficients[1,2]
    }
  }
}

R2_em_February <- R2
stderrint_em_February <- R2
stderrslp_em_February <- R2
slope_em_February <- slope
intercept_em_February <- intercept
save(slope_em_February, file = "slope_em_February.RData")
save(intercept_em_February, file = "intercept_em_February.RData")
save(R2_em_February, file = "R2_em_February.RData")
save(stderrint_em_February, file = "stderrint_em_February.RData")
save(stderrslp_em_February, file = "stderrslp_em_February.RData")
RMSE_em_February <- RMSE
save(RMSE_em_February, file = "RMSE_em_February.RData")

#---------------------------------March---------------------
vectorod <- c((as.vector(NorthAfrica_od[seq(484,724,by=8),])))
vectorem <- c((as.vector(NorthAfrica_em[seq(484,724,by=8),])))
vectorod <- vectorod*0.31
vectorem <- vectorem*0.31

slope <- matrix(0,length(em_steps),length(od_steps))
intercept <- matrix(0,length(em_steps),length(od_steps))
R2 <- matrix(0,length(em_steps),length(od_steps))
r <- matrix(0,length(em_steps),length(od_steps))
stderrint <- matrix(0,length(em_steps),length(od_steps))
stderrslp <- matrix(0,length(em_steps),length(od_steps))
RMSE <- matrix(0,length(em_steps),length(od_steps))

for (d in 1:length(od_steps))
{
  print("od_steps=")
  print(od_steps[d])
  for (em in 1:length(em_steps))
  {
    NorthAfrica_od0<- vectorod[(vectorem>=em_steps[em])&(vectorod>=od_steps[d])]
    NorthAfrica_em0<- vectorem[(vectorem>=em_steps[em])&(vectorod>=od_steps[d])]
    if (length(NorthAfrica_od0)<2)
    {
      r[em,d] <- -999
      R2[em,d] <- -999
      intercept[em,d] <- -999
      slope[em,d] <- -999
      stderrint[em,d] <- -999
      stderrslp[em,d] <- -999
      RMSE[em,d]  <- -999
    }
    else
    {
      r[em,d] <- cor(NorthAfrica_em0, NorthAfrica_od0)
      linearMod <- lm(NorthAfrica_od0 ~ NorthAfrica_em0)  # build linear regression model on full data
      RSS <- c(crossprod(linearMod$residuals))
      MSE <- RSS / length(linearMod$residuals)
      rmsex <- sqrt(MSE)
      RMSE[em,d] <- rmsex
      R2[em,d] <- summary(linearMod)$r.squared
      intercept[em,d] <- summary(linearMod)$coefficients[1,1]
      if (all(NorthAfrica_em0==rep(0,length(NorthAfrica_em0))))
      {
        slope[em,d] <- -999
        stderrslp[em,d] <- -999
      }
      else{
        slope[em,d] <- summary(linearMod)$coefficients[2,1]
        stderrslp[em,d] <- summary(linearMod)$coefficients[2,2]
      }
      stderrint[em,d] <- summary(linearMod)$coefficients[1,2]
    }
  }
}

R2_em_March <- R2
stderrint_em_March <- R2
stderrslp_em_March <- R2
slope_em_March <- slope
intercept_em_March <- intercept
save(slope_em_March, file = "slope_em_March.RData")
save(intercept_em_March, file = "intercept_em_March.RData")
save(R2_em_March, file = "R2_em_March.RData")
save(stderrint_em_March, file = "stderrint_em_March.RData")
save(stderrslp_em_March, file = "stderrslp_em_March.RData")
RMSE_em_March <- RMSE
save(RMSE_em_March, file = "RMSE_em_March.RData")

#---------------------------------April---------------------
vectorod <- c((as.vector(NorthAfrica_od[seq(732,964,by=8),])))
vectorem <- c((as.vector(NorthAfrica_em[seq(732,964,by=8),])))
vectorod <- vectorod*0.31
vectorem <- vectorem*0.31

slope <- matrix(0,length(em_steps),length(od_steps))
intercept <- matrix(0,length(em_steps),length(od_steps))
R2 <- matrix(0,length(em_steps),length(od_steps))
r <- matrix(0,length(em_steps),length(od_steps))
stderrint <- matrix(0,length(em_steps),length(od_steps))
stderrslp <- matrix(0,length(em_steps),length(od_steps))
RMSE <- matrix(0,length(em_steps),length(od_steps))

for (d in 1:length(od_steps))
{
  print("od_steps=")
  print(od_steps[d])
  for (em in 1:length(em_steps))
  {
    NorthAfrica_od0<- vectorod[(vectorem>=em_steps[em])&(vectorod>=od_steps[d])]
    NorthAfrica_em0<- vectorem[(vectorem>=em_steps[em])&(vectorod>=od_steps[d])]
    if (length(NorthAfrica_od0)<2)
    {
      r[em,d] <- -999
      R2[em,d] <- -999
      intercept[em,d] <- -999
      slope[em,d] <- -999
      stderrint[em,d] <- -999
      stderrslp[em,d] <- -999
      RMSE[em,d]  <- -999
    }
    else
    {
      r[em,d] <- cor(NorthAfrica_em0, NorthAfrica_od0)
      linearMod <- lm(NorthAfrica_od0 ~ NorthAfrica_em0)  # build linear regression model on full data
      RSS <- c(crossprod(linearMod$residuals))
      MSE <- RSS / length(linearMod$residuals)
      rmsex <- sqrt(MSE)
      RMSE[em,d] <- rmsex
      R2[em,d] <- summary(linearMod)$r.squared
      intercept[em,d] <- summary(linearMod)$coefficients[1,1]
      if (all(NorthAfrica_em0==rep(0,length(NorthAfrica_em0))))
      {
        slope[em,d] <- -999
        stderrslp[em,d] <- -999
      }
      else{
        slope[em,d] <- summary(linearMod)$coefficients[2,1]
        stderrslp[em,d] <- summary(linearMod)$coefficients[2,2]
      }
      stderrint[em,d] <- summary(linearMod)$coefficients[1,2]
    }
  }
}

R2_em_April <- R2
stderrint_em_April <- R2
stderrslp_em_April <- R2
slope_em_April <- slope
intercept_em_April <- intercept
save(slope_em_April, file = "slope_em_April.RData")
save(intercept_em_April, file = "intercept_em_April.RData")
save(R2_em_April, file = "R2_em_April.RData")
save(stderrint_em_April, file = "stderrint_em_April.RData")
save(stderrslp_em_April, file = "stderrslp_em_April.RData")
RMSE_em_April <- RMSE
save(RMSE_em_April, file = "RMSE_em_April.RData")

#---------------------------------May---------------------
vectorod <- c((as.vector(NorthAfrica_od[seq(972,1212,by=8),])))
vectorem <- c((as.vector(NorthAfrica_em[seq(972,1212,by=8),])))
vectorod <- vectorod*0.31
vectorem <- vectorem*0.31

slope <- matrix(0,length(em_steps),length(od_steps))
intercept <- matrix(0,length(em_steps),length(od_steps))
R2 <- matrix(0,length(em_steps),length(od_steps))
r <- matrix(0,length(em_steps),length(od_steps))
stderrint <- matrix(0,length(em_steps),length(od_steps))
stderrslp <- matrix(0,length(em_steps),length(od_steps))
RMSE <- matrix(0,length(em_steps),length(od_steps))

for (d in 1:length(od_steps))
{
  print("od_steps=")
  print(od_steps[d])
  for (em in 1:length(em_steps))
  {
    NorthAfrica_od0<- vectorod[(vectorem>=em_steps[em])&(vectorod>=od_steps[d])]
    NorthAfrica_em0<- vectorem[(vectorem>=em_steps[em])&(vectorod>=od_steps[d])]
    if (length(NorthAfrica_od0)<2)
    {
      r[em,d] <- -999
      R2[em,d] <- -999
      intercept[em,d] <- -999
      slope[em,d] <- -999
      stderrint[em,d] <- -999
      stderrslp[em,d] <- -999
      RMSE[em,d]  <- -999
    }
    else
    {
      r[em,d] <- cor(NorthAfrica_em0, NorthAfrica_od0)
      linearMod <- lm(NorthAfrica_od0 ~ NorthAfrica_em0)  # build linear regression model on full data
      RSS <- c(crossprod(linearMod$residuals))
      MSE <- RSS / length(linearMod$residuals)
      rmsex <- sqrt(MSE)
      RMSE[em,d] <- rmsex
      R2[em,d] <- summary(linearMod)$r.squared
      intercept[em,d] <- summary(linearMod)$coefficients[1,1]
      if (all(NorthAfrica_em0==rep(0,length(NorthAfrica_em0))))
      {
        slope[em,d] <- -999
        stderrslp[em,d] <- -999
      }
      else{
        slope[em,d] <- summary(linearMod)$coefficients[2,1]
        stderrslp[em,d] <- summary(linearMod)$coefficients[2,2]
      }
      stderrint[em,d] <- summary(linearMod)$coefficients[1,2]
    }
  }
}

R2_em_May <- R2
stderrint_em_May <- R2
stderrslp_em_May <- R2
slope_em_May <- slope
intercept_em_May <- intercept
save(slope_em_May, file = "slope_em_May.RData")
save(intercept_em_May, file = "intercept_em_May.RData")
save(R2_em_May, file = "R2_em_May.RData")
save(stderrint_em_May, file = "stderrint_em_May.RData")
save(stderrslp_em_May, file = "stderrslp_em_May.RData")
RMSE_em_May <- RMSE
save(RMSE_em_May, file = "RMSE_em_May.RData")

#---------------------------------June---------------------
vectorod <- c((as.vector(NorthAfrica_od[seq(1220,1452,by=8),])))
vectorem <- c((as.vector(NorthAfrica_em[seq(1220,1452,by=8),])))
vectorod <- vectorod*0.31
vectorem <- vectorem*0.31

slope <- matrix(0,length(em_steps),length(od_steps))
intercept <- matrix(0,length(em_steps),length(od_steps))
R2 <- matrix(0,length(em_steps),length(od_steps))
r <- matrix(0,length(em_steps),length(od_steps))
pvalue <- matrix(0,length(em_steps),length(od_steps))
stderrint <- matrix(0,length(em_steps),length(od_steps))
stderrslp <- matrix(0,length(em_steps),length(od_steps))
RMSE <- matrix(0,length(em_steps),length(od_steps))

for (d in 1:length(od_steps))
{
  print("od_steps=")
  print(od_steps[d])
  for (em in 1:length(em_steps))
  {
    NorthAfrica_od0<- vectorod[(vectorem>=em_steps[em])&(vectorod>=od_steps[d])]
    NorthAfrica_em0<- vectorem[(vectorem>=em_steps[em])&(vectorod>=od_steps[d])]
    if (length(NorthAfrica_od0)<2)
    {
      r[em,d] <- -999
      R2[em,d] <- -999
      intercept[em,d] <- -999
      slope[em,d] <- -999
      stderrint[em,d] <- -999
      stderrslp[em,d] <- -999
      RMSE[em,d]  <- -999
    }
    else
    {
      r[em,d] <- cor(NorthAfrica_em0, NorthAfrica_od0)
      linearMod <- lm(NorthAfrica_od0 ~ NorthAfrica_em0)  # build linear regression model on full data
      RSS <- c(crossprod(linearMod$residuals))
      MSE <- RSS / length(linearMod$residuals)
      rmsex <- sqrt(MSE)
      RMSE[em,d] <- rmsex
      R2[em,d] <- summary(linearMod)$r.squared
      intercept[em,d] <- summary(linearMod)$coefficients[1,1]
      if (all(NorthAfrica_em0==rep(0,length(NorthAfrica_em0))))
      {
        slope[em,d] <- -999
        stderrslp[em,d] <- -999
      }
      else{
        slope[em,d] <- summary(linearMod)$coefficients[2,1]
        stderrslp[em,d] <- summary(linearMod)$coefficients[2,2]
      }
      stderrint[em,d] <- summary(linearMod)$coefficients[1,2]
    }
  }
}

R2_em_June <- R2
stderrint_em_June <- R2
stderrslp_em_June <- R2
slope_em_June <- slope
intercept_em_June <- intercept
save(slope_em_June, file = "slope_em_June.RData")
save(intercept_em_June, file = "intercept_em_June.RData")
save(R2_em_June, file = "R2_em_June.RData")
save(stderrint_em_June, file = "stderrint_em_June.RData")
save(stderrslp_em_June, file = "stderrslp_em_June.RData")
RMSE_em_June <- RMSE
save(RMSE_em_June, file = "RMSE_em_June.RData")


#---------------------------------July---------------------
vectorod <- c((as.vector(NorthAfrica_od[seq(1460,1700,by=8),])))
vectorem <- c((as.vector(NorthAfrica_em[seq(1460,1700,by=8),])))
vectorod <- vectorod*0.31
vectorem <- vectorem*0.31

slope <- matrix(0,length(em_steps),length(od_steps))
intercept <- matrix(0,length(em_steps),length(od_steps))
R2 <- matrix(0,length(em_steps),length(od_steps))
r <- matrix(0,length(em_steps),length(od_steps))
pvalue <- matrix(0,length(em_steps),length(od_steps))
stderrint <- matrix(0,length(em_steps),length(od_steps))
stderrslp <- matrix(0,length(em_steps),length(od_steps))
RMSE <- matrix(0,length(em_steps),length(od_steps))

for (d in 1:length(od_steps))
{
  print("od_steps=")
  print(od_steps[d])
  for (em in 1:length(em_steps))
  {
    NorthAfrica_od0<- vectorod[(vectorem>=em_steps[em])&(vectorod>=od_steps[d])]
    NorthAfrica_em0<- vectorem[(vectorem>=em_steps[em])&(vectorod>=od_steps[d])]
    if (length(NorthAfrica_od0)<2)
    {
      r[em,d] <- -999
      R2[em,d] <- -999
      intercept[em,d] <- -999
      slope[em,d] <- -999
      stderrint[em,d] <- -999
      stderrslp[em,d] <- -999
      RMSE[em,d]  <- -999
    }
    else
    {
      r[em,d] <- cor(NorthAfrica_em0, NorthAfrica_od0)
      linearMod <- lm(NorthAfrica_od0 ~ NorthAfrica_em0)  # build linear regression model on full data
      RSS <- c(crossprod(linearMod$residuals))
      MSE <- RSS / length(linearMod$residuals)
      rmsex <- sqrt(MSE)
      RMSE[em,d] <- rmsex
      R2[em,d] <- summary(linearMod)$r.squared
      intercept[em,d] <- summary(linearMod)$coefficients[1,1]
      if (all(NorthAfrica_em0==rep(0,length(NorthAfrica_em0))))
      {
        slope[em,d] <- -999
        stderrslp[em,d] <- -999
      }
      else{
        slope[em,d] <- summary(linearMod)$coefficients[2,1]
        stderrslp[em,d] <- summary(linearMod)$coefficients[2,2]
      }
      stderrint[em,d] <- summary(linearMod)$coefficients[1,2]
    }
  }
}

R2_em_July <- R2
stderrint_em_July <- R2
stderrslp_em_July <- R2
slope_em_July <- slope
intercept_em_July <- intercept
save(slope_em_July, file = "slope_em_July.RData")
save(intercept_em_July, file = "intercept_em_July.RData")
save(R2_em_July, file = "R2_em_July.RData")
save(stderrint_em_July, file = "stderrint_em_July.RData")
save(stderrslp_em_July, file = "stderrslp_em_July.RData")
RMSE_em_July <- RMSE
save(RMSE_em_July, file = "RMSE_em_July.RData")


#---------------------------------August---------------------
vectorod <- c((as.vector(NorthAfrica_od[seq(1708,1948,by=8),])))
vectorem <- c((as.vector(NorthAfrica_em[seq(1708,1948,by=8),])))
vectorod <- vectorod*0.31
vectorem <- vectorem*0.31

slope <- matrix(0,length(em_steps),length(od_steps))
intercept <- matrix(0,length(em_steps),length(od_steps))
R2 <- matrix(0,length(em_steps),length(od_steps))
r <- matrix(0,length(em_steps),length(od_steps))
stderrint <- matrix(0,length(em_steps),length(od_steps))
stderrslp <- matrix(0,length(em_steps),length(od_steps))
RMSE <- matrix(0,length(em_steps),length(od_steps))

for (d in 1:length(od_steps))
{
  print("od_steps=")
  print(od_steps[d])
  for (em in 1:length(em_steps))
  {
    NorthAfrica_od0<- vectorod[(vectorem>=em_steps[em])&(vectorod>=od_steps[d])]
    NorthAfrica_em0<- vectorem[(vectorem>=em_steps[em])&(vectorod>=od_steps[d])]
    if (length(NorthAfrica_od0)<2)
    {
      r[em,d] <- -999
      R2[em,d] <- -999
      intercept[em,d] <- -999
      slope[em,d] <- -999
      stderrint[em,d] <- -999
      stderrslp[em,d] <- -999
      RMSE[em,d]  <- -999
    }
    else
    {
      r[em,d] <- cor(NorthAfrica_em0, NorthAfrica_od0)
      linearMod <- lm(NorthAfrica_od0 ~ NorthAfrica_em0)  # build linear regression model on full data
      RSS <- c(crossprod(linearMod$residuals))
      MSE <- RSS / length(linearMod$residuals)
      rmsex <- sqrt(MSE)
      RMSE[em,d] <- rmsex
      R2[em,d] <- summary(linearMod)$r.squared
      intercept[em,d] <- summary(linearMod)$coefficients[1,1]
      if (all(NorthAfrica_em0==rep(0,length(NorthAfrica_em0))))
      {
        slope[em,d] <- -999
        stderrslp[em,d] <- -999
      }
      else{
        slope[em,d] <- summary(linearMod)$coefficients[2,1]
        stderrslp[em,d] <- summary(linearMod)$coefficients[2,2]
      }
      stderrint[em,d] <- summary(linearMod)$coefficients[1,2]
    }
  }
}

R2_em_August <- R2
stderrint_em_August <- R2
stderrslp_em_August <- R2
slope_em_August <- slope
intercept_em_August <- intercept
save(slope_em_August, file = "slope_em_August.RData")
save(intercept_em_August, file = "intercept_em_August.RData")
save(R2_em_August, file = "R2_em_August.RData")
save(stderrint_em_August, file = "stderrint_em_August.RData")
save(stderrslp_em_August, file = "stderrslp_em_August.RData")
RMSE_em_August <- RMSE
save(RMSE_em_August, file = "RMSE_em_August.RData")

#---------------------------------Setember---------------------
vectorod <- c((as.vector(NorthAfrica_od[seq(1956,2188,by=8),])))
vectorem <- c((as.vector(NorthAfrica_em[seq(1956,2188,by=8),])))
vectorod <- vectorod*0.31
vectorem <- vectorem*0.31

slope <- matrix(0,length(em_steps),length(od_steps))
intercept <- matrix(0,length(em_steps),length(od_steps))
R2 <- matrix(0,length(em_steps),length(od_steps))
r <- matrix(0,length(em_steps),length(od_steps))
pvalue <- matrix(0,length(em_steps),length(od_steps))
stderrint <- matrix(0,length(em_steps),length(od_steps))
stderrslp <- matrix(0,length(em_steps),length(od_steps))
RMSE <- matrix(0,length(em_steps),length(od_steps))

for (d in 1:length(od_steps))
{
  print("od_steps=")
  print(od_steps[d])
  for (em in 1:length(em_steps))
  {
    NorthAfrica_od0<- vectorod[(vectorem>=em_steps[em])&(vectorod>=od_steps[d])]
    NorthAfrica_em0<- vectorem[(vectorem>=em_steps[em])&(vectorod>=od_steps[d])]
    if (length(NorthAfrica_od0)<2)
    {
      r[em,d] <- -999
      R2[em,d] <- -999
      intercept[em,d] <- -999
      slope[em,d] <- -999
      stderrint[em,d] <- -999
      stderrslp[em,d] <- -999
      RMSE[em,d]  <- -999
    }
    else
    {
      r[em,d] <- cor(NorthAfrica_em0, NorthAfrica_od0)
      linearMod <- lm(NorthAfrica_od0 ~ NorthAfrica_em0)  # build linear regression model on full data
      RSS <- c(crossprod(linearMod$residuals))
      MSE <- RSS / length(linearMod$residuals)
      rmsex <- sqrt(MSE)
      RMSE[em,d] <- rmsex
      R2[em,d] <- summary(linearMod)$r.squared
      intercept[em,d] <- summary(linearMod)$coefficients[1,1]
      if (all(NorthAfrica_em0==rep(0,length(NorthAfrica_em0))))
      {
        slope[em,d] <- -999
        stderrslp[em,d] <- -999
      }
      else{
        slope[em,d] <- summary(linearMod)$coefficients[2,1]
        stderrslp[em,d] <- summary(linearMod)$coefficients[2,2]
      }
      stderrint[em,d] <- summary(linearMod)$coefficients[1,2]
    }
  }
}

R2_em_Setember <- R2
stderrint_em_Setember <- R2
stderrslp_em_Setember <- R2
slope_em_Setember <- slope
intercept_em_Setember <- intercept
save(slope_em_Setember, file = "slope_em_Setember.RData")
save(intercept_em_Setember, file = "intercept_em_Setember.RData")
save(R2_em_Setember, file = "R2_em_Setember.RData")
save(stderrint_em_Setember, file = "stderrint_em_Setember.RData")
save(stderrslp_em_Setember, file = "stderrslp_em_Setember.RData")
RMSE_em_Setember <- RMSE
save(RMSE_em_Setember, file = "RMSE_em_Setember.RData")


#---------------------------------October---------------------
vectorod <- c((as.vector(NorthAfrica_od[seq(2196,2436,by=8),])))
vectorem <- c((as.vector(NorthAfrica_em[seq(2196,2436,by=8),])))
vectorod <- vectorod*0.31
vectorem <- vectorem*0.31

slope <- matrix(0,length(em_steps),length(od_steps))
intercept <- matrix(0,length(em_steps),length(od_steps))
R2 <- matrix(0,length(em_steps),length(od_steps))
r <- matrix(0,length(em_steps),length(od_steps))
stderrint <- matrix(0,length(em_steps),length(od_steps))
stderrslp <- matrix(0,length(em_steps),length(od_steps))
RMSE <- matrix(0,length(em_steps),length(od_steps))

for (d in 1:length(od_steps))
{
  print("od_steps=")
  print(od_steps[d])
  for (em in 1:length(em_steps))
  {
    NorthAfrica_od0<- vectorod[(vectorem>=em_steps[em])&(vectorod>=od_steps[d])]
    NorthAfrica_em0<- vectorem[(vectorem>=em_steps[em])&(vectorod>=od_steps[d])]
    if (length(NorthAfrica_od0)<2)
    {
      r[em,d] <- -999
      R2[em,d] <- -999
      intercept[em,d] <- -999
      slope[em,d] <- -999
      stderrint[em,d] <- -999
      stderrslp[em,d] <- -999
      RMSE[em,d]  <- -999
    }
    else
    {
      r[em,d] <- cor(NorthAfrica_em0, NorthAfrica_od0)
      linearMod <- lm(NorthAfrica_od0 ~ NorthAfrica_em0)  # build linear regression model on full data
      RSS <- c(crossprod(linearMod$residuals))
      MSE <- RSS / length(linearMod$residuals)
      rmsex <- sqrt(MSE)
      RMSE[em,d] <- rmsex
      R2[em,d] <- summary(linearMod)$r.squared
      intercept[em,d] <- summary(linearMod)$coefficients[1,1]
      if (all(NorthAfrica_em0==rep(0,length(NorthAfrica_em0))))
      {
        slope[em,d] <- -999
        stderrslp[em,d] <- -999
      }
      else{
        slope[em,d] <- summary(linearMod)$coefficients[2,1]
        stderrslp[em,d] <- summary(linearMod)$coefficients[2,2]
      }
      stderrint[em,d] <- summary(linearMod)$coefficients[1,2]
    }
  }
}

R2_em_October <- R2
stderrint_em_October <- R2
stderrslp_em_October <- R2
slope_em_October <- slope
intercept_em_October <- intercept
save(slope_em_October, file = "slope_em_October.RData")
save(intercept_em_October, file = "intercept_em_October.RData")
save(R2_em_October, file = "R2_em_October.RData")
save(stderrint_em_October, file = "stderrint_em_October.RData")
save(stderrslp_em_October, file = "stderrslp_em_October.RData")
RMSE_em_October <- RMSE
save(RMSE_em_October, file = "RMSE_em_October.RData")

#---------------------------------November---------------------
vectorod <- c((as.vector(NorthAfrica_od[seq(2444,2676,by=8),])))
vectorem <- c((as.vector(NorthAfrica_em[seq(2444,2676,by=8),])))
vectorod <- vectorod*0.31
vectorem <- vectorem*0.31

slope <- matrix(0,length(em_steps),length(od_steps))
intercept <- matrix(0,length(em_steps),length(od_steps))
R2 <- matrix(0,length(em_steps),length(od_steps))
r <- matrix(0,length(em_steps),length(od_steps))
pvalue <- matrix(0,length(em_steps),length(od_steps))
stderrint <- matrix(0,length(em_steps),length(od_steps))
stderrslp <- matrix(0,length(em_steps),length(od_steps))
RMSE <- matrix(0,length(em_steps),length(od_steps))

for (d in 1:length(od_steps))
{
  print("od_steps=")
  print(od_steps[d])
  for (em in 1:length(em_steps))
  {
    NorthAfrica_od0<- vectorod[(vectorem>=em_steps[em])&(vectorod>=od_steps[d])]
    NorthAfrica_em0<- vectorem[(vectorem>=em_steps[em])&(vectorod>=od_steps[d])]
    if (length(NorthAfrica_od0)<2)
    {
      r[em,d] <- -999
      R2[em,d] <- -999
      intercept[em,d] <- -999
      slope[em,d] <- -999
      stderrint[em,d] <- -999
      stderrslp[em,d] <- -999
      RMSE[em,d]  <- -999
    }
    else
    {
      r[em,d] <- cor(NorthAfrica_em0, NorthAfrica_od0)
      linearMod <- lm(NorthAfrica_od0 ~ NorthAfrica_em0)  # build linear regression model on full data
      RSS <- c(crossprod(linearMod$residuals))
      MSE <- RSS / length(linearMod$residuals)
      rmsex <- sqrt(MSE)
      RMSE[em,d] <- rmsex
      R2[em,d] <- summary(linearMod)$r.squared
      intercept[em,d] <- summary(linearMod)$coefficients[1,1]
      if (all(NorthAfrica_em0==rep(0,length(NorthAfrica_em0))))
      {
        slope[em,d] <- -999
        stderrslp[em,d] <- -999
      }
      else{
        slope[em,d] <- summary(linearMod)$coefficients[2,1]
        stderrslp[em,d] <- summary(linearMod)$coefficients[2,2]
      }
      stderrint[em,d] <- summary(linearMod)$coefficients[1,2]
    }
  }
}

R2_em_November <- R2
stderrint_em_November <- R2
stderrslp_em_November <- R2
slope_em_November <- slope
intercept_em_November <- intercept
save(slope_em_November, file = "slope_em_November.RData")
save(intercept_em_November, file = "intercept_em_November.RData")
save(R2_em_November, file = "R2_em_November.RData")
save(stderrint_em_November, file = "stderrint_em_November.RData")
save(stderrslp_em_November, file = "stderrslp_em_November.RData")
RMSE_em_November <- RMSE
save(RMSE_em_November, file = "RMSE_em_November.RData")

#---------------------------------December---------------------
vectorod <- c((as.vector(NorthAfrica_od[seq(2684,2924,by=8),])))
vectorem <- c((as.vector(NorthAfrica_em[seq(2684,2924,by=8),])))
vectorod <- vectorod*0.31
vectorem <- vectorem*0.31

slope <- matrix(0,length(em_steps),length(od_steps))
intercept <- matrix(0,length(em_steps),length(od_steps))
R2 <- matrix(0,length(em_steps),length(od_steps))
r <- matrix(0,length(em_steps),length(od_steps))
stderrint <- matrix(0,length(em_steps),length(od_steps))
stderrslp <- matrix(0,length(em_steps),length(od_steps))
RMSE <- matrix(0,length(em_steps),length(od_steps))

for (d in 1:length(od_steps))
{
  print("od_steps=")
  print(od_steps[d])
  for (em in 1:length(em_steps))
  {
    NorthAfrica_od0<- vectorod[(vectorem>=em_steps[em])&(vectorod>=od_steps[d])]
    NorthAfrica_em0<- vectorem[(vectorem>=em_steps[em])&(vectorod>=od_steps[d])]
    if (length(NorthAfrica_od0)<2)
    {
      r[em,d] <- -999
      R2[em,d] <- -999
      intercept[em,d] <- -999
      slope[em,d] <- -999
      stderrint[em,d] <- -999
      stderrslp[em,d] <- -999
      RMSE[em,d]  <- -999
    }
    else
    {
      r[em,d] <- cor(NorthAfrica_em0, NorthAfrica_od0)
      linearMod <- lm(NorthAfrica_od0 ~ NorthAfrica_em0)  # build linear regression model on full data
      RSS <- c(crossprod(linearMod$residuals))
      MSE <- RSS / length(linearMod$residuals)
      rmsex <- sqrt(MSE)
      RMSE[em,d] <- rmsex
      R2[em,d] <- summary(linearMod)$r.squared
      intercept[em,d] <- summary(linearMod)$coefficients[1,1]
      if (all(NorthAfrica_em0==rep(0,length(NorthAfrica_em0))))
      {
        slope[em,d] <- -999
        stderrslp[em,d] <- -999
      }
      else{
        slope[em,d] <- summary(linearMod)$coefficients[2,1]
        stderrslp[em,d] <- summary(linearMod)$coefficients[2,2]
      }
      stderrint[em,d] <- summary(linearMod)$coefficients[1,2]
    }
  }
}

R2_em_December <- R2
stderrint_em_December <- R2
stderrslp_em_December <- R2
slope_em_December <- slope
intercept_em_December <- intercept
save(slope_em_December, file = "slope_em_December.RData")
save(intercept_em_December, file = "intercept_em_December.RData")
save(R2_em_December, file = "R2_em_December.RData")
save(stderrint_em_December, file = "stderrint_em_December.RData")
save(stderrslp_em_December, file = "stderrslp_em_December.RData")
RMSE_em_December <- RMSE
save(RMSE_em_December, file = "RMSE_em_December.RData")

NorthAfrica_od0 <- vectorod[vectorem != 0]
NorthAfrica_em0<- vectorem[vectorem != 0]
vectorod <- vectorod[vectorem==0]
vectorem <- vectorem[vectorem==0]

length(vectorod)
length(vectorem)
length(NorthAfrica_od0)
length(NorthAfrica_em0)
datos <- data.frame(vectorem,vectorod)

r[1] <- cor(NorthAfrica_em0, NorthAfrica_od0)
linearMod <- lm(NorthAfrica_od0 ~ NorthAfrica_em0)  # build linear regression model on full data
summary(linearMod)
R2[1] <- summary(linearMod)$r.squared
pvalue[1] <- summary(linearMod)$coefficients[,4] 
intercept[1] <- summary(linearMod)$coefficients[1,1] 
slope[1] <- summary(linearMod)$coefficients[2,1] 
stderrint[1] <- summary(linearMod)$coefficients[1,2]
stderrslp[1] <- summary(linearMod)$coefficients[2,2]