rm(list=ls(all=TRUE)) # clear all variables
graphics.off() # clear all graphics
# Visual Search
# Greg Francis
# PSY 646
# 10 September 2018
# fit a linear model that predicts response time as a function of the number of distractors
# It can take a few minutes for the code to get moving. During this time, the program is rewriting the model into another programming language and compiling it. Once it starts running, you will see output on the console window.
# load full data file
VSdata<-read.csv(file="VisualSearch.csv",header=TRUE,stringsAsFactors=FALSE)
# pull out just the trials for the first participant's conjunctive target present condition
VSdata2<-subset(VSdata, VSdata$Participant=="Francis200S16-2" & VSdata$Target=="Absent" & VSdata$DistractorType=="Conjunction")
#
# load the brms library
library(brms)
# By default brms provides priors that are adapted to fit within the range of your data. Generally, this produces results similar to standard linear regression. Later, we will introduce informative priors
model1 = brm(RT_ms ~ NumberDistractors, data = VSdata2, iter = 2000, warmup = 200, chains = 3,
thin = 2 )
# print out summary of model
print(summary(model1))
# Compute the highst posterior density interval (HPDI) for each parameter
# Load up some useful libraries for analyzing posterior distribution
library(broom)
library(coda)
print( tidyMCMC(model1$fit, conf.int = TRUE, conf.method = "HPDinterval") )
# Compare to confidence interval
fit <- lm(RT_ms ~ NumberDistractors, data = VSdata2)
print(fit)
print(confint(fit) )
# Build a model that generates a linear fit for TargetAbsent and Target Present conditions. Use common intercept
# Pull out just the trials for the first participant's conjunctive condition (includes Target present and Target absent)
VSdata2<-subset(VSdata, VSdata$Participant=="Francis200S16-2" & VSdata$DistractorType=="Conjunction")
# By default brms provides priors that are adapted to fit within the range of your data. Generally, this produces results similar to standard linear regression. Later, we will introduce informative priors
model2 <- brm(RT_ms ~ Target:NumberDistractors, data = VSdata2, iter = 2000, warmup = 200, chains = 3)
# print out summary of model
print(summary(model2))
# Check on model convergence and posteriors
plot(model2)
# Plot fitting line
dev.new()
plot(marginal_effects(model2), points = TRUE)
# Build a model that generates a linear fit for TargetAbsent and Target Present conditions. Use different intercepts for the target conditions.
# By default brms provides priors that are adapted to fit within the range of your data. Generally, this produces results similar to standard linear regression. Later, we will introduce informative priors
model3 <- brm(RT_ms ~ Target*NumberDistractors, data = VSdata2, iter = 2000, warmup = 200, chains = 3)
# print out summary of model
print(summary(model3))
# Check on model convergence and posteriors
dev.new()
plot(model3)
# Plot fitting line
dev.new()
plot(marginal_effects(model3), points = TRUE)
#-----------------------------------
# Show effects of different slopes for a different set of data
# Build a model that generates a linear fit for TargetAbsent and Target Present conditions. Use common intercept
# Pull out just the trials for the first participant's conjunctive condition (includes Target present and Target absent)
VSdata2<-subset(VSdata, VSdata$Participant=="Francis200S16-5" & VSdata$DistractorType=="Conjunction")
# By default brms provides priors that are adapted to fit within the range of your data. Generally, this produces results similar to standard linear regression. Later, we will introduce informative priors
model2 <- brm(RT_ms ~ Target:NumberDistractors, data = VSdata2, iter = 2000, warmup = 200, chains = 3)
# print out summary of model
print(summary(model2))
# Check on model convergence and posteriors
plot(model2)
# Plot fitting line
dev.new()
plot(marginal_effects(model2), points = TRUE)
# Build a model that generates a linear fit for TargetAbsent and Target Present conditions. Use different intercepts for the target conditions.
# By default brms provides priors that are adapted to fit within the range of your data. Generally, this produces results similar to standard linear regression. Later, we will introduce informative priors
model3 <- brm(RT_ms ~ Target*NumberDistractors, data = VSdata2, iter = 2000, warmup = 200, chains = 3)
# print out summary of model
print(summary(model3))
# Check on model convergence and posteriors
dev.new()
plot(model3)
# Plot fitting line
dev.new()
plot(marginal_effects(model3), points = TRUE)
#-----------------
# Build a model using an exgaussian rather than a gaussian (better for response times)
VSdata2<-subset(VSdata, VSdata$Participant=="Francis200S16-2" & VSdata$DistractorType=="Conjunction")
model4 <- brm(RT_ms ~ Target*NumberDistractors, family = exgaussian(link = "identity"), data = VSdata2, iter = 2000, warmup = 200, chains = 3)
dev.new()
plot(model4)
plot(marginal_effects(model4), points = TRUE)
# With more iterations
model4 <- brm(RT_ms ~ Target*NumberDistractors, family = exgaussian(link = "identity"), data = VSdata2, iter = 8000, warmup = 2000, chains = 3)
dev.new()
plot(model4)
plot(marginal_effects(model4), points = TRUE)