#Libraries

library("readxl")
## Warning: package 'readxl' was built under R version 4.0.2
library(ggplot2)
library(dplyr)
## Warning: package 'dplyr' was built under R version 4.0.2
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
data = read_excel("Prov_total.xlsx")
data
## # A tibble: 95 x 5
##    Date                   GP    WC   KZN    EC
##    <dttm>              <dbl> <dbl> <dbl> <dbl>
##  1 2020-03-20 00:00:00   109    56    24     0
##  2 2020-03-21 00:00:00   125    74    27     1
##  3 2020-03-22 00:00:00   132    88    36     2
##  4 2020-03-23 00:00:00   207   100    60     2
##  5 2020-03-24 00:00:00   302   113    80     2
##  6 2020-03-25 00:00:00   319   183    91     2
##  7 2020-03-26 00:00:00   409   229   134     5
##  8 2020-03-27 00:00:00   471   250   145     8
##  9 2020-03-28 00:00:00   533   271   156    10
## 10 2020-03-29 00:00:00   584   310   167    12
## # ... with 85 more rows
p = ggplot() + 
  geom_line(data=data, aes(x = Date, y = GP), color = "blue") +
  geom_line(data=data, aes(x = Date, y = WC), color = "red") +
  geom_line(data=data, aes(x = Date, y = KZN), color = "green") +
  geom_line(data=data, aes(x = Date, y = EC), color = "yellow") +
  xlab('Dates') +
  ylab('Comfirmed cases')

print(p)

#Linear programming

#Gauteng

duration.freq = table(data$GP)
duration.cumfreq = cumsum(duration.freq)
duration.cumrelfreq = duration.cumfreq / nrow(data)
duration.cumrelfreq
##        109        125        132        207        302        319        409 
## 0.01052632 0.02105263 0.03157895 0.04210526 0.05263158 0.06315789 0.07368421 
##        471        533        584        618        633        645        663 
## 0.08421053 0.09473684 0.10526316 0.11578947 0.12631579 0.13684211 0.14736842 
##        672        693        704        713        748        782        795 
## 0.15789474 0.16842105 0.17894737 0.18947368 0.20000000 0.21052632 0.22105263 
##        801        813        865        890        909        930        969 
## 0.23157895 0.24210526 0.25263158 0.26315789 0.27368421 0.28421053 0.29473684 
##       1018       1101       1148       1170       1199       1224       1252 
## 0.30526316 0.31578947 0.32631579 0.33684211 0.34736842 0.35789474 0.36842105 
##       1281       1304       1331       1353       1377       1408       1446 
## 0.37894737 0.38947368 0.40000000 0.41052632 0.42105263 0.43157895 0.44210526 
##       1507       1598       1624       1661       1697       1720       1804 
## 0.45263158 0.46315789 0.47368421 0.48421053 0.49473684 0.50526316 0.51578947 
##       1851       1910       1952       1971       2014       2074       2135 
## 0.52631579 0.53684211 0.54736842 0.55789474 0.56842105 0.57894737 0.58947368 
##       2210       2262       2329       2343       2361       2400       2453 
## 0.60000000 0.61052632 0.62105263 0.63157895 0.64210526 0.65263158 0.66315789 
##       2521       2633       2773       2993       3043       3167       3329 
## 0.67368421 0.68421053 0.69473684 0.70526316 0.71578947 0.72631579 0.73684211 
##       3583       3773       4003       4231       4276       4567       4845 
## 0.74736842 0.75789474 0.76842105 0.77894737 0.78947368 0.80000000 0.81052632 
##       5215       5626       5946       6258       6546       7195       7953 
## 0.82105263 0.83157895 0.84210526 0.85263158 0.86315789 0.87368421 0.88421053 
##       8781       9897      11164      12193      13023      14620      15898 
## 0.89473684 0.90526316 0.91578947 0.92631579 0.93684211 0.94736842 0.95789474 
##      17261      18978      20822      22341 
## 0.96842105 0.97894737 0.98947368 1.00000000
Regre = lm(data$GP~duration.cumrelfreq, data = data) 
summary(Regre)
## 
## Call:
## lm(formula = data$GP ~ duration.cumrelfreq, data = data)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -3102.8 -2255.6  -703.2  1424.5 12724.3 
## 
## Coefficients:
##                     Estimate Std. Error t value Pr(>|t|)    
## (Intercept)          -2672.8      638.5  -4.186 6.44e-05 ***
## duration.cumrelfreq  12289.5     1097.3  11.200  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 3087 on 93 degrees of freedom
## Multiple R-squared:  0.5742, Adjusted R-squared:  0.5697 
## F-statistic: 125.4 on 1 and 93 DF,  p-value: < 2.2e-16
duration.freq1 = table(data$WC)
duration.cumfreq1 = cumsum(duration.freq1)
duration.cumrelfreq1 = duration.cumfreq1 / nrow(data)
duration.cumrelfreq1
##         56         74         88        100        113        183        229 
## 0.01052632 0.02105263 0.03157895 0.04210526 0.05263158 0.06315789 0.07368421 
##        250        271        310        324        325        326        353 
## 0.08421053 0.09473684 0.10526316 0.11578947 0.12631579 0.13684211 0.14736842 
##        374        433        454        462        479        495        515 
## 0.15789474 0.16842105 0.17894737 0.18947368 0.20000000 0.21052632 0.22105263 
##        541        546        587        617        643        657        675 
## 0.23157895 0.24210526 0.25263158 0.26315789 0.27368421 0.28421053 0.29473684 
##        717        836        868        940       1010       1079       1279 
## 0.30526316 0.31578947 0.32631579 0.33684211 0.34736842 0.35789474 0.36842105 
##       1413       1514       1608       1737       1870       2135       2342 
## 0.37894737 0.38947368 0.40000000 0.41052632 0.42105263 0.43157895 0.44210526 
##       2507       2700       3044       3362       3609       3760       3994 
## 0.45263158 0.46315789 0.47368421 0.48421053 0.49473684 0.50526316 0.51578947 
##       4497       4809       5168       5621       6105       6713       7235 
## 0.52631579 0.53684211 0.54736842 0.55789474 0.56842105 0.57894737 0.58947368 
##       7798       8404       9294      10035      10639      11262      12153 
## 0.60000000 0.61052632 0.62105263 0.63157895 0.64210526 0.65263158 0.66315789 
##      12888      13826      14740      15396      15829      16893      17754 
## 0.67368421 0.68421053 0.69473684 0.70526316 0.71578947 0.72631579 0.73684211 
##      18906      20160      21382      22567      23583      24657      27006 
## 0.74736842 0.75789474 0.76842105 0.77894737 0.78947368 0.80000000 0.81052632 
##      28807      30379      31824      33568      34819      36021      37422 
## 0.82105263 0.83157895 0.84210526 0.85263158 0.86315789 0.87368421 0.88421053 
##      38926      40605      42539      44143      45357      46555      47522 
## 0.89473684 0.90526316 0.91578947 0.92631579 0.93684211 0.94736842 0.95789474 
##      48706      50173      51441      52554 
## 0.96842105 0.97894737 0.98947368 1.00000000
Regre2 = lm(data$WC~duration.cumrelfreq1, data = data) 
summary(Regre2)
## 
## Call:
## lm(formula = data$WC ~ duration.cumrelfreq1, data = data)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
##  -9224  -6801  -1742   6114  16596 
## 
## Coefficients:
##                      Estimate Std. Error t value Pr(>|t|)    
## (Intercept)            -11841       1571  -7.538 3.09e-11 ***
## duration.cumrelfreq1    47799       2699  17.708  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 7595 on 93 degrees of freedom
## Multiple R-squared:  0.7713, Adjusted R-squared:  0.7688 
## F-statistic: 313.6 on 1 and 93 DF,  p-value: < 2.2e-16
duration.freq2 = table(data$KZN)
duration.cumfreq2 = cumsum(duration.freq2)
duration.cumrelfreq2 = duration.cumfreq2 / nrow(data)
duration.cumrelfreq2
##         24         27         36         60         80         91        134 
## 0.01052632 0.02105263 0.03157895 0.04210526 0.05263158 0.06315789 0.07368421 
##        145        156        167        171        179        186        206 
## 0.08421053 0.09473684 0.10526316 0.11578947 0.12631579 0.13684211 0.14736842 
##        215        232        246        257        306        354        387 
## 0.15789474 0.16842105 0.17894737 0.18947368 0.20000000 0.21052632 0.22105263 
##        412        418        443        465        489        519        539 
## 0.23157895 0.24210526 0.25263158 0.26315789 0.27368421 0.28421053 0.29473684 
##        591        604        617        639        671        758        807 
## 0.30526316 0.31578947 0.32631579 0.33684211 0.34736842 0.35789474 0.36842105 
##        841        847        863        902        919        956        980 
## 0.37894737 0.38947368 0.40000000 0.41052632 0.42105263 0.43157895 0.44210526 
##       1006       1051       1076       1106       1142       1189       1204 
## 0.45263158 0.46315789 0.47368421 0.48421053 0.49473684 0.50526316 0.51578947 
##       1253       1308       1353       1372       1394       1413       1444 
## 0.52631579 0.53684211 0.54736842 0.55789474 0.56842105 0.57894737 0.58947368 
##       1482       1498       1543       1567       1616       1650       1693 
## 0.60000000 0.61052632 0.62105263 0.63157895 0.64210526 0.65263158 0.66315789 
##       1735       1777       1815       1882       1927       2186       2349 
## 0.67368421 0.68421053 0.69473684 0.70526316 0.71578947 0.72631579 0.73684211 
##       2428       2476       2545       2565       2637       2707       2869 
## 0.74736842 0.75789474 0.76842105 0.77894737 0.78947368 0.80000000 0.81052632 
##       2936       3016       3108       3175       3255       3347       3472 
## 0.82105263 0.83157895 0.84210526 0.85263158 0.86315789 0.87368421 0.88421053 
##       3573       3763       3874       3959       4048       4238       4360 
## 0.89473684 0.90526316 0.91578947 0.92631579 0.93684211 0.94736842 0.95789474 
##       4538       4806       5030       5278 
## 0.96842105 0.97894737 0.98947368 1.00000000
Regre3 = lm(data$KZN~duration.cumrelfreq2, data = data) 
summary(Regre3)
## 
## Call:
## lm(formula = data$KZN ~ duration.cumrelfreq2, data = data)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -581.11 -345.86  -86.95  248.10 1530.98 
## 
## Coefficients:
##                      Estimate Std. Error t value Pr(>|t|)    
## (Intercept)           -678.38      92.66  -7.321 8.64e-11 ***
## duration.cumrelfreq2  4425.40     159.24  27.791  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 448 on 93 degrees of freedom
## Multiple R-squared:  0.8925, Adjusted R-squared:  0.8914 
## F-statistic: 772.4 on 1 and 93 DF,  p-value: < 2.2e-16
duration.freq4 = table(data$EC)
duration.cumfreq4 = cumsum(duration.freq4)
duration.cumrelfreq4 = duration.cumfreq4 / nrow(data)
duration.cumrelfreq4
##          0          1          2          5          8         10         12 
## 0.01052632 0.02105263 0.06315789 0.07368421 0.08421053 0.09473684 0.12631579 
##         15         17         21         25         31         32         39 
## 0.13684211 0.14736842 0.15789474 0.16842105 0.17894737 0.18947368 0.20000000 
##         45         61         68         69         88        104        174 
## 0.21052632 0.22105263 0.23157895 0.24210526 0.25263158 0.26315789 0.27368421 
##        199        220        246        270        293        310        345 
## 0.28421053 0.29473684 0.30526316 0.31578947 0.32631579 0.33684211 0.34736842 
##        377        417        480        488        535        588        616 
## 0.35789474 0.36842105 0.37894737 0.38947368 0.40000000 0.41052632 0.42105263 
##        630        647        691        732        774        814        838 
## 0.43157895 0.44210526 0.45263158 0.46315789 0.47368421 0.48421053 0.49473684 
##        849        929        989       1078       1218       1356       1504 
## 0.50526316 0.51578947 0.52631579 0.53684211 0.54736842 0.55789474 0.56842105 
##       1534       1569       1662       1812       1936       2052       2135 
## 0.57894737 0.58947368 0.60000000 0.61052632 0.62105263 0.63157895 0.64210526 
##       2215       2324       2459       2569       2690       2748       2864 
## 0.65263158 0.66315789 0.67368421 0.68421053 0.69473684 0.70526316 0.71578947 
##       3047       3306       3583       3759       3927       4111       4324 
## 0.72631579 0.73684211 0.74736842 0.75789474 0.76842105 0.77894737 0.78947368 
##       4526       4936       5240       5629       5974       6341       6760 
## 0.80000000 0.81052632 0.82105263 0.83157895 0.84210526 0.85263158 0.86315789 
##       7154       7868       8615       9250      10027      10597      11039 
## 0.87368421 0.88421053 0.89473684 0.90526316 0.91578947 0.92631579 0.93684211 
##      11919      12639      13506      14721      15751      16895 
## 0.94736842 0.95789474 0.96842105 0.97894737 0.98947368 1.00000000
#Regre4 = lm(data$EC~duration.cumrelfreq4, data = data) 
#summary(Regre4)
beta <- coef(Regre)[2]
beta
## duration.cumrelfreq 
##             12289.5
alpha <- exp(coef(Regre)[1])
theta <- min(data$GP) * 0.5
#model <- nls(y ~ alpha * exp(beta * x) + theta , data =data$GP, start = start)