Select a city:

Clear["Global`*"]

city="Sokode";
city="Lome";
city="Atakpame";
city="Sotouboua";
city="Dapaong";
city="KoumaKonda";
city="Niamtougou";
city="Tabligbo";
city="Mango";
city="Kara";

Importing Data

datahome="/Users/longa/Desktop/classes/000\ 2018Spring/mat375/FinalProject/Data/v1/"; (* School *)
datahome="/home/andy/classes/2018Spring/MathModeling/projects/Final/data/" ; (* home *)
datahome="http://ceadserv1.nku.edu/longa/classes/mat375/finalproject/Data/v1/" ; (* web *)
file=StringJoin[datahome,city,"_Clean.xlsx"];
list=Import[file];

list[[1,1,All]]

These are the Global Togo Temperature Models:

minModel[decYear_,latitude_,longitude_,elevation_]:=
1.219*Sin[decYear*2*Pi]+(-.457)*Cos[decYear*2*Pi]+(.404)*Sin[decYear*4*Pi]+(-.839)*Cos[decYear*4*Pi]+(.088)*Sin[decYear*6*Pi]+(-.230)*Cos[decYear*6*Pi]+(.030)*decYear+(-6.192)*latitude+(-.430)*2*latitude*longitude+.414*(((latitude))^2)+14.716*longitude+
(-3.604)*(((longitude))^2)+(-.002)*elevation+(-18.912);
maxModel[decYear_,latitude_,longitude_,elevation_]:=
1.466*Sin[decYear*2*Pi]+(2.637)*Cos[decYear*2*Pi]+(-.130)*Sin[decYear*4*Pi]+(-.859)*Cos[decYear*4*Pi]+(.232)*Sin[decYear*6*Pi]+(-.330)*Cos[decYear*6*Pi]+(.019)*decYear+(7.882)*latitude+(.309)*2*latitude*longitude+(-.451)*(((latitude))^2)+(-11.159)*longitude+
(3.102)*(((longitude))^2)+(-.009)*elevation+(-33.112);

minModelConstant[latitude_,longitude_,elevation_]:=
(-6.192)*latitude+(-.430)*2*latitude*longitude+.414*(((latitude))^2)+14.716*longitude+
(-3.604)*(((longitude))^2)+(-.002)*elevation+(-18.912);
maxModelConstant[latitude_,longitude_,elevation_]:=
(7.882)*latitude+(.309)*2*latitude*longitude+(-.451)*(((latitude))^2)+(-11.159)*longitude+
(3.102)*(((longitude))^2)+(-.009)*elevation+(-33.112);

A couple of functions we need:

seekPeriods[p_,data_]:=Module[{lm=LinearModelFit[data,{x,Sin[2π x/p],Cos[2π x/p]},x]},
lm["AdjustedRSquared"]]
OurSquared[real_,pred_]:= 
Module[{mean=Mean[real],nvals=Length[real]},
1-((∑_(i=1)^nvals)(((real[[i]]-pred[[i]]))^2))/((∑_(i=1)^nvals)(((real[[i]]-mean))^2))
]

longitude=list[[1,2,5]]
latitude=list[[1,2,6]]
elevation=list[[1,2,7]]

decYear=Rest[Flatten[list[[1,All,9]]]];
minTemp=Rest[Flatten[list[[1,All,10]]]];
maxTemp=
Rest[Flatten[list[[1,All,If[city == "KoumaKonda",4,3]]]]];
nvals=Length[maxTemp]

Global Togo Model for Minimum Data

realdatamin=Table[{decYear[[i]],minTemp[[i]]},{i,1,nvals}];
pretempmin=minModel[decYear,latitude,longitude,elevation];
predictedDataMin=Table[{decYear[[i]],pretempmin[[i]]},{i,1,nvals}];
ListLinePlot[{predictedDataMin,realdatamin},PlotLabel->city]

Residuals/RSquared for Minimum Model

resids=minTemp-pretempmin;

ListPlot[resids,PlotLabel->city]

OurSquared[minTemp,pretempmin]

Global Togo Model for Maximum Data

realdatamax=Table[{decYear[[i]],maxTemp[[i]]},{i,1,nvals}];
pretempmax=maxModel[decYear,latitude,longitude,elevation];
predictedDataMax=Table[{decYear[[i]],pretempmax[[i]]},{i,1,nvals}];
ListLinePlot[{predictedDataMax,realdatamax},PlotLabel->city]

Residuals/RSquared for Maximum Model

residsmax=maxTemp-pretempmax;

ListPlot[residsmax,PlotLabel->city]

rsquaredMax=OurSquared[maxTemp,pretempmax]

Local Togo Model for Minimum Data

minmodel=LinearModelFit[realdatamin,{Sin[2*Pi*x],Cos[2*Pi*x],Sin[4*Pi*x],Cos[4*Pi*x],Sin[6*Pi*x],Cos[6*Pi*x],x},x]

Show[ListPlot[realdatamin,PlotLabel->city],Plot[minmodel[x],{x,1961,2015}],Frame->True]

minmodel["ParameterTable"]

minmodel["AdjustedRSquared"]

MatrixForm[minmodel["ParameterConfidenceIntervals"]]
MatrixForm[{minModelConstant[latitude,longitude,elevation],
1.219,
-.457,
.404,
-.839,
.088,
-.230,
.030}]

The coefficients given by the Togo model were only within the confidence intervals for the 

 terms.

Local Togo Model for Maximum Data

maxmodel=LinearModelFit[realdatamax,{Sin[2*Pi*x],Cos[2*Pi*x],Sin[4*Pi*x],Cos[4*Pi*x],Sin[6*Pi*x],Cos[6*Pi*x],x},x]

Show[ListPlot[realdatamax,PlotLabel->city],Plot[maxmodel[x],{x,1961,2015}],Frame->True]

maxmodel["ParameterTable"]

MatrixForm[maxmodel["ParameterConfidenceIntervals"]]
MatrixForm[{maxModelConstant[latitude,longitude,elevation],
1.466,
2.637,
-.130,
-.859,
.232,
-.330,
.019}]

When we compared the Togo Model coefficients with our confidence intervals, the confidence intervals contained 

 of the Global Togo coefficients.

maxmodel["AdjustedRSquared"]

Finding Periods with Real Minimum Data

Plot[seekPeriods[p,realdatamin],{p,1/6,1.1},PlotRange->{All,{0,.3}},PlotLabel->city]
Plot[seekPeriods[p,realdatamin],{p,1.1,5},PlotRange->{All,{0,.3}},PlotLabel->city]
Plot[seekPeriods[p,realdatamin],{p,5,25},PlotRange->{All,{0,.3}},PlotLabel->city]

From these plots, we think we should include a period of 

 year.

Finding Periods with Minimum Residuals

Plot[seekPeriods[p,resids],{p,1/6,1.1},PlotRange->{All,{0,.3}},PlotLabel->city]
Plot[seekPeriods[p,resids],{p,1.1,25},PlotRange->{All,{0,.3}},PlotLabel->city]
Plot[seekPeriods[p,resids],{p,11,13},PlotRange->{All,{0,.3}},PlotLabel->city]

Finding Periods with Real Maximum Data

Plot[seekPeriods[p,realdatamax],{p,1/10,1.1},PlotRange->{All,{0,.3}},PlotLabel->city]
Plot[seekPeriods[p,realdatamax],{p,1.1,5},PlotRange->{All,{0,.3}},PlotLabel->city]
Plot[seekPeriods[p,realdatamax],{p,5,25},PlotRange->{All,{0,.3}},PlotLabel->city]
Plot[seekPeriods[p,realdatamax],{p,11,13},PlotRange->{All,{0,.3}},PlotLabel->city]

From these plots, we think we should include a period of 1 year.

Finding Periods with Maximum Residuals

Plot[seekPeriods[p,residsmax],{p,1/6,1.1},PlotRange->{All,{0,.3}},PlotLabel->city]
Plot[seekPeriods[p,residsmax],{p,1.1,25},PlotRange->{All,{0,.3}},PlotLabel->city]
Plot[seekPeriods[p,residsmax],{p,11,13},PlotRange->{All,{0,.3}},PlotLabel->city]

From these plots, we think we should add a period of 12 years, 1/4 year, and 1/2 year.

Adding these Periods into Minimum Model

finalmin=LinearModelFit[realdatamin,{
x,
Sin[2*π*x],Cos[2*π*x],
Sin[2*π*x/(1/2)],Cos[2*π*x/(1/2)],
Sin[2*π*x/(1/3)],Cos[2*π*x/(1/3)],
Sin[2*π*x/(1/4)],Cos[2*π*x/(1/4)],
Sin[2*π*x/(1/5)],Cos[2*π*x/(1/5)],
Sin[2*π*x/(6)],Cos[2*π*x/(6)],
Sin[2*π*x/(12)],Cos[2*π*x/(12)]
},x]
Show[ListPlot[realdatamin,PlotLabel->city],Plot[finalmin[x],{x,1961,2015}],Frame->True]
finalmin["ParameterTable"]
finalmin["ParameterConfidenceIntervals"]
finalmin["RSquared"]
ListPlot[finalmin["FitResiduals"],PlotLabel->city]

finalResidualsMin=finalmin["FitResiduals"];
Plot[seekPeriods[p,finalResidualsMin],{p,1/6,1.1},PlotRange->All,PlotLabel->city]
Plot[seekPeriods[p,finalResidualsMin],{p,1.1,20},PlotRange->All,PlotLabel->city]

Adding these Periods into Maximum Model

finalmax=LinearModelFit[realdatamax,{
x,
Sin[2*π*x],Cos[2*π*x],
Sin[2*π*x/(1/2)],Cos[2*π*x/(1/2)],
Sin[2*π*x/(1/3)],Cos[2*π*x/(1/3)],
Sin[2*π*x/(1/4)],Cos[2*π*x/(1/4)],
Sin[2*π*x/(1/5)],Cos[2*π*x/(1/5)],
Sin[2*π*x/(6)],Cos[2*π*x/(6)],
Sin[2*π*x/(12)],Cos[2*π*x/(12)]
},x]
Show[ListPlot[realdatamax,PlotLabel->city],Plot[finalmax[x],{x,1961,2015}],Frame->True,PlotRange->All]
finalmax["ParameterTable"]
finalmax["ParameterConfidenceIntervals"]
finalmax["RSquared"]
ListPlot[finalmax["FitResiduals"],PlotLabel->city]

finalResidualsMax=finalmax["FitResiduals"];
Plot[seekPeriods[p,finalResidualsMax],{p,1/6,1},PlotRange->All,PlotLabel->city]
Plot[seekPeriods[p,finalResidualsMax],{p,1.1,20},PlotRange->All,PlotLabel->city]