Skip to content

Instantly share code, notes, and snippets.

@klpn
klpn / restim.R
Last active February 28, 2021 18:39
Plot regional or national Rt for COVID-19 based on FHM data to reproduce FHM reports
# Plot regional or national Rt for COVID-19 based on FHM data, to reproduce the
# results published on https://www.folkhalsomyndigheten.se/smittskydd-beredskap/utbrott/aktuella-utbrott/covid-19/statistik-och-analyser/analys-och-prognoser/
# Adapted from:
# Churches (2020, Feb. 18). Tim Churches Health Data Science Blog: Analysing COVID-19 (2019-nCoV) outbreak data with R - part 1. Retrieved from https://timchurches.github.io/blog/posts/2020-02-18-analysing-covid-19-2019-ncov-outbreak-data-with-r-part-1/, CC BY-SA
library(readxl)
library(EpiEstim)
# path to latest FHM Excel file
secov <- read_excel("data/FHM/FHM_latest.xlsx")
secov$Statistikdatum <- as.Date(secov$Statistikdatum)
@klpn
klpn / tagged-pres.diff
Created February 13, 2020 10:54
Change to Pandoc 2.8 beamer template to create PDF/A-2a
*** default.beamer 2020-02-13 11:50:17.541486784 +0100
--- tagged-pres.beamer 2020-02-13 11:32:29.142235033 +0100
***************
*** 10,15 ****
--- 10,20 ----
$endif$
$endif$
%
+ \begin{filecontents*}{\jobname.xmpdata}
+ \Title{$title$}
@klpn
klpn / pdfa2a.diff
Created June 22, 2019 06:47
Change to Pandoc 2.7.2 ConTeXt template to produce PDF/A-2a.
*** default.context 2019-06-22 08:40:19.015364224 +0200
--- default-pdfa2a.context 2019-06-22 08:43:07.763916183 +0200
***************
*** 27,33 ****
% make chapter, section bookmarks visible when opening document
\placebookmarks[chapter, section, subsection, subsubsection, subsubsubsection, subsubsubsubsection][chapter, section]
\setupinteractionscreen[option=bookmark]
- \setuptagging[state=start]
$if(papersize)$
@klpn
klpn / WordToPDFA.psm1
Last active July 30, 2018 08:13
Word to PDF/A conversion adapted for Archivematica
function ConvertTo-WordPDFA
{
Param(
[Parameter(Mandatory=$true,Position=0)][String]$dpath
)
$word_app = New-Object -ComObject Word.Application
$doc = $word_app.Documents.Open($dpath)
$dpath_dir = [System.IO.Path]::GetDirectoryName($dpath)
$dpath_base = [System.IO.Path]::GetFileNameWithoutExtension($dpath)
$pdf_fname = $dpath_base + ".pdf"
@klpn
klpn / sedor.jl
Last active September 8, 2017 15:09
Analyze Swedish cause-of-death and population data (assumed to be in data/2017-9-10-4Amod.csv and data/mfsv16.csv) transformed to CSV files.
using DataFrames
deaths = readtable(normpath("data", "2017-9-10-4Amod.csv"))
ages = DataFrame(age = collect(0:20),
agest = [0;0;1;collect(5:5:90)],
age2 =[0;1;fill(2,3);fill(3,6);fill(4,4);fill(5,2);fill(6,4)])
ages2 = by(ages[:,[:agest, :age2]], :age2,
df -> DataFrame(agest=minimum(df[:agest])))
pop = DataFrame()
pop1 = readtable(normpath("data", "mfsv16.csv"))
@klpn
klpn / meni.jl
Last active April 28, 2017 10:20
Create chart showing sum of hospitalized cases with meningococcal disease (ICD-10 A39 as main diagnosis) in Sweden over years, using data from The National Board of Health and Welfare
using DataFrames, PyPlot
m = readtable("meni9815.csv")
magg = aggregate(m, :Ålder, sum)
bar(magg[:Ålder], magg[:Kvinnor_sum], -2, align="edge", label = "Kvinnor")
bar(magg[:Ålder], magg[:Män_sum], 2, align="edge", label = "Män")
grid(1)
legend()
xlabel("Ålder")
ylabel("Antal (summa för ålder över år)")
title("Sjukhusvårdade meningokockinfektion Sverige 1998\u20132015")
@klpn
klpn / allskinsv15.csv
Last active April 1, 2017 15:48
Create chart on incidence for all cancer and skin cancer vs age Sweden 2015, based on data from http://www.socialstyrelsen.se/statistik/statistikdatabas/cancer
age fall mall fskin mskin
0 0.0002006 0.0002459 0.0 0.0
5 9.22e-5 0.0001006 0.0 0.0
10 0.0001424 0.0001568 0.0 0.0
15 0.00022940000000000002 0.0002389 0.0 0.0
20 0.0003875 0.00036030000000000003 9.3e-6 0.0
25 0.0007676 0.0004501 9.2e-6 0.0
30 0.0011704 0.0007615 1.34e-5 0.0
35 0.0023091 0.0009373000000000001 2.02e-5 9.7e-6
40 0.0032355 0.0013991 3.74e-5 3.0299999999999998e-5
@klpn
klpn / amplt.jl
Last active November 6, 2016 13:34
Calculate lifetime cancer risk in Sweden (using data from Statstics Sweden and National Board of Health and Welfare), built on the method defined by Sasieni et. al, (doi:10.1038/bjc.2011.250).
using LifeTable, DataFrames
function AmpLt(inframe, sex, rate = "inc")
age = inframe[1]
pop = inframe[2]
acd = inframe[3]
cd = inframe[4]
cc = inframe[5]
if rate == "inc"
ncol = cc
@klpn
klpn / cervc4290rate2.csv
Created October 16, 2016 08:14
Calculate life table for Swedish females 2013 with and without decrease cervical cancer mortality (data from Statistics Sweden, http://www.statistikdatabasen.scb.se/goto/sv/ssd/LivslangdEttariga, and WHO via mortchartgen).
We can make this file beautiful and searchable if this error is corrected: It looks like row 3 should actually have 36 columns, instead of 22. in line 2.
Year,Pop1,Pop2,Pop3,Pop4,Pop5,Pop6,Pop7,Pop8,Pop9,Pop10,Pop11,Pop12,Pop13,Pop14,Pop15,Pop16,Pop17,Pop18,Pop19,Pop20,Pop21,Pop22,Pop23,Pop24,Pop25,Pop26,Pop36sum,Pop222sum,Pop2325sum,Pop38mean,Pop914mean,Pop1518mean,Pop1920mean,Pop2122mean,List
1951,3.268342161613885e-05,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.923816852635629e-05,2.2123893805309735e-05,2.982848620432513e-05,5.200594353640416e-05,8.46091861402095e-05,4.908522980812137e-05,8.060453400503778e-05,6.872852233676975e-05,6.93000693000693e-05,3.5842293906810036e-05,5.46448087431694e-05,0.00010416666666666667,4.464285714285714e-05,,,,0.0,3.260653831977091e-05,4.464285714285714e-05,0.0,2.053274867873255e-05,7.07568680725346e-05,5.2571181603439665e-05,7.940573770491804e-05,07A
1952,3.8315247790580604e-05,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,4.42282176028306e-06,0.0,3.637686431429611e-05,4.514672686230248e-05,5.167958656330749e-05,5.966587112171838e-05,0.00010048055919615553,8.91089108910891e-05,7.369614512471656e-05,7.407407407407407e-05,9.70873786407767e-05,6.
@klpn
klpn / Masironi70.csv
Last active September 24, 2016 14:00
Connect Masironi (1970), https://www.ncbi.nlm.nih.gov/pmc/articles/pmid/5309508/, with WHO mortality data (assume MySQL database set up according to my mortchartgen repo).
ctryname country totfat satfat suc m55mort f55mort
Jordan 3170 18.5 3.7 11 49 19
El Salvador 2190 19.5 5.5 51 28
Taiwan 3070 15.3 4.8 4.2 58 53
Philippines 3300 9.6 3.7 5.5 89 40
Guatemala 2250 15.1 4.1 115 84
Mexico 2310 24.8 7.9 122 65
Greece 4140 26.5 5.4 4.9 162 61
Japan 3160 14.5 3.4 7.3 165 92
Panama 2350 22.6 6.8 8.7 170 76