Advertisement
s243a

uk_hospital_waves.m

Feb 4th, 2023
1,054
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 2.40 KB | None | 0 0
  1. #---------Load hospitalization data from our world in data.---------
  2. pkg load io
  3. C=csv2cell('c:\current-covid-patients-hospital.csv');
  4.  
  5. #---------Filter out data for a specific country.---------
  6.  
  7. %I=find(strcmp({C{:,1}},"Canada")==1)
  8. #I=find(strcmp({C{:,1}},"United Kingdom")==1);
  9. I=find(strcmp(C(2:end,1),"United Kingdom")==1);
  10. C2=C(I,:);
  11. #---------Calculate cummulative hospitalizations---------
  12. C2(:,5)=num2cell(cumsum([C2{:,4}]));
  13. ##---------hospitalizations with average removed---------
  14. #C2(:,6)=num2cell([C2{:,4}]./([C2{:,5}]./[size(C2,1)]));
  15. ##---------cumulative hospitalizations with average removed---------
  16. #C2(:,7)=num2cell(cumsum([C2{:,6}]));
  17. ##----------select data range for a specif peak
  18. d1=datestr('2022-05-31');
  19. d2=datestr('2022-09-14');
  20.  
  21. #---------Convert String date to date object------------
  22. C2(:,3)=cellfun(@(m) datestr(m),C2(:,3),'UniformOutput',false);
  23. #---------For Plotting we need the datenum format.
  24. C2(:,6)=cellfun(@(m) datenum(m),
  25.                 C2(:,3),'UniformOutput',false);
  26.  
  27. #---------Make function to filter date range------------
  28. function y=date_rng(x,a,b)
  29.   x2=datenum(x);
  30.   a2=datenum(a);
  31.   b2=datenum(b);
  32.   y=x2>a2 & x2<b2;
  33. end
  34.  
  35. dp=datestr('2022-07-15');
  36. I_dp=find([cellfun(@(m) isequal(m,dp),
  37.           C2(:,3),'UniformOutput',false){:}]);
  38.  
  39. #Maybe use is between function instead of date_rng https://www.mathworks.com/help/matlab/matlab_prog/compare-dates-and-time.html
  40.  
  41. #----------Remove data outside of date range (e.g. a specific peak
  42. I2=find([cellfun(@(m) date_rng(m,d1,d2),
  43.          C2(:,3),'UniformOutput',false){:}]);
  44. C3=C2(I2,:);
  45.  
  46. #---------Subtract the min value so cumulative changes are only for this peak.---------
  47. C3(:,7)=num2cell([C3{:,4}]-min([C3{:,4}]));
  48. #---------cumulative hospitalizations with average removed---------
  49. C3(:,8)=num2cell(cumsum([C3{:,4}]));
  50. #----------select data range for a specif peak
  51.  
  52. #Use scale factors to put hospitalization levles on the same graph as cumulative levels.
  53. m_dpd_max=max([C3{:,4}]); # hospitalization scale factor
  54. m_dpd_min=min([C3{:,4}]); # hospitalization scale factor
  55. m_dpd_detla=m_dpd_max-m_dpd_min;
  56.  
  57. m_cd_max=max([C3{:,8}]);  # cumulative scale factor
  58.  
  59.  
  60. C3(:,8)=num2cell([C3{:,8}]*m_dpd_detla/m_cd_max+m_dpd_min);
  61.  
  62. plot([C3{:,6}],[C3{:,8}]);
  63. hold on
  64. plot([C3{:,6}],[C3{:,4}]);
  65. datetick("x",'YYYY-mm-DD');
  66. hold off
  67. #datetick(gca)
  68. #https://octave.sourceforge.io/octave/function/datetick.html
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement