Skip to content

Instantly share code, notes, and snippets.

View n8thangreen's full-sized avatar

Dr Nathan Green n8thangreen

View GitHub Profile
################
# Markov model #
################
## Model set-up ----
t_names <- c("without_drug", "with_drug")
n_treatments <- length(t_names)
s_names <- c("Asymptomatic_disease", "Progressive_disease", "Dead")
ce_res <- ce_markov(start_pop = c(0, 1),
p_matrix,
c_matrix,
q_matrix)
#
ce_markov <- function(start_pop,
p_matrix,
c_matrix,
q_matrix,
@n8thangreen
n8thangreen / tables_and_plots_lecture.R
Created March 12, 2018 09:06
Tables and plots lecture
R: Presenting outputs
========================================================
author: Dr N Green
date: March 2018
autosize: false
width: 2440
height: 1200
css: custom.css
#' Decision Tree Expected Values
#'
#' \deqn{C_i = \sum p_{ij} (C_{ij} + C_j)}
#'
#' @param vals Values on each edge (array) e.g. costs or QALYs
#' @param p Transition probabilities (array)
#'
#' @return expected value at each node (list)
#' @export
#' Branch Joint Probability
#'
#' @param probs Branch conditional probabilities (array)
#'
#' @return
#' @export
#'
#' @examples
#' branch_joint_probs(probs) * cost
#'
library(Rfast)
library(utils)
prob_test_outcome_combination <- function(prob,
n_choose) {
p_comb <- combn(x = prob, m = n_choose)
n_comb <- combn(x = 1 - prob, m = length(prob) - n_choose)
two_test_parallel_AND <- function(sens, spec) {
list(
sensitivity = sens*sens,
specificity = spec + spec - spec*spec)
}
three_test_parallel_AND_2 <- function(sens, spec) {
list(
@n8thangreen
n8thangreen / R introduction - Lecture 2.R
Last active December 15, 2016 09:48
Imperial College London MSc Public Health practical
## ----setup, include=FALSE------------------------------------------------
knitr::opts_chunk$set(echo = TRUE)
## ---- eval=T,echo=T------------------------------------------------------
a <- 1
b <- 2
c <- -1
d <- 2
# Check if two values are the same. Note the difference between
@n8thangreen
n8thangreen / R introduction - Lecture 1.R
Last active December 15, 2016 09:48
Imperial College London MSc Public Health practical
## ----setup, include=FALSE------------------------------------------------
knitr::opts_chunk$set(echo = TRUE)
## ---- echo=T, eval=T-----------------------------------------------------
2 + 2
## ---- echo=T, eval=T-----------------------------------------------------
a <- 2 + 2
## ---- echo=T,eval=T------------------------------------------------------
#--run in the old version of R
setwd("C:/Temp/")
packages <- installed.packages()[,"Package"]
save(packages, file="Rpackages")
Followed by this in the new version:
#--run in the new version
setwd("C:/Temp/")
load("Rpackages")
for (p in setdiff(packages, installed.packages()[,"Package"]))