Skip to content

Instantly share code, notes, and snippets.

View fusaroli's full-sized avatar

Riccardo Fusaroli fusaroli

View GitHub Profile
library(tidyverse)
library(brms)
library(patchwork)
## Define amount of data
items <- 30
chains <- 5
raters <- 30
## Define parameter values
# Assuming you have a matrix (CorMat_S) with a structure like a correlation matrix
# Load the libraries (NB probably some superfluous ones, I haven't double-checked)
pacman::p_load(
# General utility functions
tidyverse,
# Plotting
ggplot2,
scales,
ggstance,
library(tidyverse)
library(data.table)
temp <- read_delim("~/Downloads/temp.csv", delim = ";")
setDT(temp)
r <- ":acute interstitial pneumonitis:|:alveolar lung disease:|:alveolar proteinosis:|:alveolitis:|:alveolitis necrotising:|:autoimmune lung disease:|:bronchiolitis:|:combined pulmonary fibrosis and emphysema:|:diffuse alveolar damage:|:eosinophilia myalgia syndrome:|:eosinophilic granulomatosis with polyangiitis:|:eosinophilic pneumonia:|:eosinophilic pneumonia acute:|:eosinophilic pneumonia chronic:|:hypersensitivity pneumonitis:|:idiopathic interstitial pneumonia:|:idiopathic pneumonia syndrome:|:idiopathic pulmonary fibrosis:|:immune-mediated pneumonitis:|:interstitial lung disease:|:lung infiltration:|:lung opacity:|:necrotising bronchiolitis:|:obliterative bronchiolitis:|:pneumonitis:|:progressive massive fibrosis:|:pulmonary fibrosis:|:pulmonary necrosis:|:pulmonary toxicity:|:pulmonary vasculitis:|:small airways disease:|:transfusion-related acute lung injury:|:acute lung injury:|:acute respira
pacman::p_load(tidyverse, brms)
seed <- 1981 # Defining a seed so the results are always the same
samplesize <- 300 # Defining the amount of datapoints
n <- 1000 # Defining number of simulations
MaxScore <- 8 # Defining max rating
MinScore <- 1 # Defining min rating
## Regression to the mean?
Regression2Mean <- 0.7 # 1st to 2nd in the empirical data : 0.668444
@fusaroli
fusaroli / Q1P1
Created March 18, 2020 11:36
Livecoding of the first question
pacman::p_load(tidyverse, brms)
# Prepare the data
d <- Ass3
#summary(d)
d$Diagnosis <- plyr::revalue(as.character(d$Diagnosis),
c("0"="Controls", "1"="Schizophrenia"))
@fusaroli
fusaroli / trial.r
Last active December 20, 2019 10:24
Monotonic issue
# Load libraries
library(brms)
library(tidyverse)
# Load data
data <- read_csv("temp_data.csv")
# Define model
# Load libraries
library(brms)
library(tidyverse)
# Load data
data <- read_csv("temp_data.csv")
# Define model
Model2_Language_f <- bf(Outcome ~ 0 + language + language:Diagnosis +
@fusaroli
fusaroli / LooErrorExample.R
Last active July 4, 2019 08:04
Reproducible example for Loo error
## Load libraries
pacman::p_load(tidyverse,brms)
## Load data
data <- read_csv("data.csv")
## Define formulas
Model1_f <- bf(Outcome ~ Diagnosis + (1|ID))
Model2_f <- bf(Outcome ~ Diagnosis + (1|p|ID),
# Defining formulas
PitchMean_f <- bf(Pitch_MeanS ~ Diagnosis + (1|ID))
PitchMean_Language_f <- bf(Pitch_MeanS ~ 0 + language + language:Diagnosis + (1|ID))
PitchMean_Age_f <- bf(Pitch_MeanS ~ 0 + Diagnosis + AgeS:Diagnosis + (1|ID))
PitchMean_Gender_f <- bf(Pitch_MeanS ~ 0 + Gender + Gender:Diagnosis + (1|ID))
PitchMean_AgeGender_f <- bf(Pitch_MeanS ~ 0 + Gender + Gender:AgeS + Gender:Diagnosis + Gender:AgeS:Diagnosis + (1 | ID))
PitchMean_Full_f <- bf(Pitch_MeanS ~ 1 + Diagnosis * (Gender*AgeS + language) + (1 | ID))
# Fittig and saving models
PitchMean_SkepticModel_Trial <- brmsFun(Name="PitchMean_SkepticModel_Trial",formula=PitchMean_f,data=d_trial,prior=SkepticPrior)
library(brms)
zero_one_inflated_beta2 <- custom_family(
"zero_one_inflated_beta2", dpars = c("mu", "phi","zero","one"),
links = c("logit", "log","logit","logit"), lb = c(NA, 0, 0, 0),
ub = c(NA, NA, 1, 1), type = "real"
)
stan_funs <- "