ClearAll

data=Import["C:\\Users\\austi\\OneDrive\\Desktop\\School Stuff\\MAT375 Models\\CleanedKKData.csv"]

Length[data]

rainfall=Rest[data[[All,{4}]]]
year=Rest[data[[All,{2}]]]
mintemp=Rest[data[[All,{5}]]]
maxtemp=Rest[data[[All,{6}]]]

years=year-1961

Rain vs. Time

rainfallTable=Partition[Flatten[Table[{years[[i]],rainfall[[i]]},{i,1,601}]],2];

rainfallAndTime=LinearModelFit[rainfallTable,{x,Cos[2Pi*x],Sin[2Pi*x]},x,ConfidenceLevel->0.95]

rainplot=Plot[rainfallAndTime[x],{x,0,55},PlotRange->{0,500}];
raintimeplot=ListPlot[rainfallTable];
Show[{rainplot,raintimeplot}]

ListPlot[rainfallAndTime["FitResiduals"],Filling->Axis]
rainfallAndTime["ParameterConfidenceIntervals"]
rainfallAndTime["ParameterTable"]
rainfallAndTime["RSquared"]

ListPlot[rainfallAndTime["FitResiduals"]]
rainfallAndTime["ParameterConfidenceIntervals"]
rainfallAndTime["ParameterTable"]
rainfallAndTime["RSquared"]
Histogram[rainfallAndTime["FitResiduals"]]

Partition[Flatten[Transpose[{mintemp,maxtemp}],2],2];

𝒟=SmoothKernelDistribution[Partition[Flatten[Transpose[{mintemp,maxtemp}],2],2]];
dens=ContourPlot[PDF[𝒟,{x,y}]//Evaluate,{x,Min[mintemp],Max[mintemp]},{y,Min[maxtemp],Max[maxtemp]},PlotRange->All,PlotPoints->50,Contours->15,ColorFunction->"TemperatureMap",AxesLabel->Automatic]

Show[dens,AxesLabel->{None,None},FrameLabel->{{HoldForm[Max Temperatures],None},{HoldForm[Min Temperatures],None}},PlotLabel->None,LabelStyle->{GrayLevel[0]}]

ListPointPlot3D[Partition[Flatten[Transpose[{mintemp,maxtemp,rainfall}]],3],PlotRange->All,ColorFunction->"TemperatureMap",AxesLabel-> {HoldForm[Min Temperatures],HoldForm[Max Temperatures], HoldForm[Rainfall]}]

dens=Plot3D[PDF[𝒟,{x,y}]//Evaluate,{x,Min[mintemp],Max[mintemp]},{y,Min[maxtemp],Max[maxtemp]},PlotRange->All,PlotPoints->50,Contours->15,ColorFunction->"TemperatureMap", AxesLabel-> {HoldForm[Min Temperatures],HoldForm[Max Temperatures], HoldForm[Probability]}]
ListPointPlot3D[Partition[Flatten[Transpose[{mintemp,maxtemp,rainfall}]],3],PlotRange->All,ColorFunction->"TemperatureMap",AxesLabel-> {HoldForm[Min Temperatures],HoldForm[MaxTemperatures], HoldForm[Rainfall]},Filling->Bottom]

raintable=Partition[Flatten[Table[{i+1960,j,rainfall[[12*(i-1)+j]]},{i,1,50,1},{j,1,12,1}]],3];
ListPointPlot3D[raintable,PlotRange->All,ColorFunction->"TemperatureMap", AxesLabel-> {HoldForm[Year],HoldForm[Month], HoldForm[Rainfall]}]

Temperature Range

temperatureRange=Flatten[Table[{maxtemp[[i]]-mintemp[[i]]},{i,1,601}]]

timeTempRange=Partition[Flatten[Table[{years[[i]],temperatureRange[[i]]},{i,1,601}]],2]

tempRangeModel=LinearModelFit[timeTempRange,{x,(x^2),Cos[2Pi*x],Sin[2Pi*x],Cos[2Pi*x*4],Sin[2Pi*x*4],Cos[2Pi*x*3],Sin[2Pi*x*3],Cos[(((2Pi*x))/28)],Sin[(((2Pi*x))/28)]},x,ConfidenceLevel->0.95]

rangeModelplot=Plot[tempRangeModel[x],{x,0,12},PlotRange->{0,25}]
rangeplot=ListPlot[timeTempRange]
Show[{rangeModelplot,rangeplot}]

ListPlot[tempRangeModel["FitResiduals"],Filling->Axis]
tempRangeModel["ParameterConfidenceIntervals"]
tempRangeModel["ParameterTable"]
tempRangeModel["RSquared"]

Histogram[tempRangeModel["FitResiduals"]]

*Full: 2, 4, 6, 8, 12

rainfallAndTimefull=LinearModelFit[rainfallTable,{x,Cos[2Pi*x],Sin[2Pi*x],Cos[12Pi*x],Sin[12Pi*x],Cos[8Pi*x],Sin[8Pi*x],Cos[6Pi*x],Sin[6Pi*x],Cos[4Pi*x],Sin[4Pi*x]},x,ConfidenceLevel->0.95]

rainplotfull=Plot[rainfallAndTimefull[x],{x,0,12},PlotRange->{0,2500}]
raintimeplotfull=ListPlot[rainfallTable]
Show[{rainplotfull,raintimeplotfull}]

ListPlot[rainfallAndTimefull["FitResiduals"],Filling->Axis]
rainfallAndTimefull["ParameterConfidenceIntervals"]
rainfallAndTimefull["ParameterTable"]
rainfallAndTimefull["RSquared"]

({{"", "Estimate", "Standard Error", "t\[Hyphen]Statistic", "P\[Hyphen]Value"}, {1, 144.4025918127576`, 5.403097684518641`, 26.725889525653162`, 1.1686718387312534`*^-103}, {x, -0.3422188845690366`, 0.16536004693431727`, -2.0695379017700053`, 0.03893110815477015`}, {Cos[2 π x], -84.09386982334894`, 3.700457682734317`, -22.725261854963538`, 1.442957544202984`*^-82}, {Sin[2 π x], -22.70803502150144`, 3.7244557248389536`, -6.097007643306955`, 1.9558952614148335`*^-9}, {Cos[12 π x], -2.7372706122621688`*^13, 4.576009383195004`*^13, -0.5981785400865997`, 0.5499507476985352`}, {Sin[12 π x], 0.9550552311533723`, 3.2442618096119427`, 0.2943829096418115`, 0.7685690428785834`}, {Cos[8 π x], 21.798736741641903`, 3.7091647388810856`, 5.8769934139452`, 6.9959085158586554`*^-9}, {Sin[8 π x], -6.480594788735111`, 3.7082105226616804`, -1.7476340000468658`, 0.08104852294418313`}, {Cos[6 π x], -13.607428729553131`, 3.7107084860921375`, -3.667070259103953`, 0.00026754770559310936`}, {Sin[6 π x], 17.60411350669638`, 3.7086911462042993`, 4.746718670470444`, 2.598408444329446`*^-6}, {Cos[4 π x], -3.981446849896295`*^11, 6.655950661483323`*^11, -0.598178540134942`, 0.5499507476662806`}, {Sin[4 π x], -7.800930190101676`, 3.6844400189652817`, -2.117263451147848`, 0.034655993642062226`}})

*y: 2, 6, 8

rainfallAndTimey=LinearModelFit[rainfallTable,{x,Cos[2Pi*x],Sin[2Pi*x],Cos[8Pi*x],Sin[8Pi*x],Cos[6Pi*x],Sin[6Pi*x]},x,ConfidenceLevel->0.95]

rainploty=Plot[rainfallAndTimey[x],{x,0,12},PlotRange->{0,450}]
raintimeploty=ListPlot[rainfallTable]
Show[{rainploty,raintimeploty}]

Histogram[rainfallAndTimey["FitResiduals"]]

Manipulate[
rainmodelploty=Plot[rainfallAndTimey[x],{x,t,(t+n)},PlotRange->{0,500},PlotLabel->"Model Versus Rain Data",AxesLabel->{"Year's Since 1961","Rainfall"}];
Show[rainmodelploty,raintimeplot],
{{t,0},0,53,1},
{{n,1},1,5,1}]