Skip to content

Instantly share code, notes, and snippets.

View fusaroli's full-sized avatar

Riccardo Fusaroli fusaroli

View GitHub Profile
@fusaroli
fusaroli / Lme4PowerAnalysis.R
Last active September 25, 2017 16:58
Power analysis for linear mixed effects model (lme4)
# TO DO: ADJUST TO OTHER PARAMETERS (E:G: WHAT IF THERE ARE STIMULUS EFFECTS? OR MORE FIX EFFS?)
createNewData <- function (J,K,model){
# J is the number of subjects
# K is the number of visits
# HP is the hyperparameter matrix from get Hyperparam
# TO DO: LOOP THROUGH ALL FE ROWS AND AUTOMATICALLY EXTRACT NAMES OF FIXED EFFECTS AND ESTIMATES
fe <- fixef(model)
Intercept <- fe[1] #intercept
bVisit <- fe[2] #visit
bDiagnosis <- fe[3] #diagnosis
// contest model
data{
int N_choices; // amount of data points
int N_items; // unique stimuli
int N_raters; // unique raters
int y[N_choices]; // outcome
int item1[N_choices]; // which stimulus 1 in each trials
int item2[N_choices]; // which stimulus 2 in each trials
int rater[N_choices]; // which rater in each trials
real Period[N_items]; // item feature: period
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 <- "
# 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)
@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),
# 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 / 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
@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"))
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
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