Extract data from ROIs

Load in 3rd party packages


In [25]:
%matplotlib inline
import matplotlib.pylab as plt

In [26]:
import os.path as op
import re

import numpy as np
import pandas as pd
from scipy import stats

import nibabel as nb
from nibabel import load
import nipype.interfaces.fsl as fsl

import nitime
import nitime.fmri.io as io
from nitime.timeseries import TimeSeries
from nitime.analysis import SpectralAnalyzer, FilterAnalyzer, NormalizationAnalyzer

import moss
import seaborn as sns
sns.set(context='poster', style='whitegrid')
import lyman
from lyman import tools

%load_ext rpy2.ipython
%R require(lme4)
%R require(lmerTest)


The rpy2.ipython extension is already loaded. To reload it, use:
  %reload_ext rpy2.ipython
Out[26]:
<Vector - Python:0x129c57bd8 / R:0x11e3bf4c8>
[       1]

Helper functions


In [27]:
def determine_mask(mask, hemi, roi_type, contrast):
    if contrast == 'anat':
        if hemi == 'bilat':
            mask_name = mask
        else: # unilateral
            if roi_type == 'aseg':
                mask_name = mask
            else: # label
                mask_name = hemi + '.' + mask
    else: # func contrast
        if hemi == 'bilat':
            mask_name = mask + '-' + contrast
        else: # unilateral
            if roi_type == 'contrast-aseg':
                mask_name = mask + '-' + contrast
            else: # label
                mask_name = hemi + '.' + mask + '-' + contrast
    
    return mask_name

Set up parameters


In [331]:
experiment = 'objfam'
altmodel = 'submem_nuisance_comb'
subject_list = 'subids_subset_no23or19.txt' # Stored in data_dir
mni_space = True
if mni_space:
    rois = 'rois_mni.csv'
    regspace = 'mni'
    smoothing = 'smoothed'
else:
    rois = 'rois.csv' # Stored in analysis_dir for altmodel
    regspace = 'epi'
    smoothing = 'unsmoothed'
    
input_file = 'cope1.nii.gz'

Gather project & experiment data


In [332]:
project = lyman.gather_project_info()
exp = lyman.gather_experiment_info(experiment, altmodel)
exp_name = "-".join([experiment, altmodel])

subj_path = op.join(project['data_dir'], subject_list)
mask_path = op.join(project['analysis_dir'], exp_name, rois)

subjects = np.loadtxt(subj_path, str)
masks = np.array(pd.read_csv(mask_path, header=None))

Group Analysis


In [334]:
# set up storage dataframe
df = pd.DataFrame(columns=('subid','roi','mask_vox','hemi','cond','value'))

for subid in subjects:

    contrast_list = exp['condition_names']
    contrast_names = exp['condition_names']
    
    for contrast,label in zip(contrast_list, contrast_names):
        
        fmri_file = op.join(project['analysis_dir'], exp_name, 
                            subid, 'ffx', regspace, smoothing, 
                            contrast, input_file)
        
        # Read data using nibabel:
        fmri_data = load(fmri_file)
        func_arr = fmri_data.get_data()

        for mask, hemi, roi_type, roi_id, contrast in masks: 
        
            mask_name = determine_mask(mask, hemi, roi_type, contrast)
        
            if mni_space:
                mask_file = op.join(project['data_dir'], 'fsaverage', 
                                    'masks', mask_name + '.nii.gz')
            else:    
                mask_file = op.join(project['data_dir'], subid, 
                                    'masks', mask_name + '.nii.gz')
                
            # Read in the mask as bool using nibabel:
            mask_data = load(mask_file)
            mask_arr = mask_data.get_data().astype(bool)
            num_voxels = mask_arr.sum()
            
            # Mask the data
            func_masked = func_arr[mask_arr]
#             print func_masked.shape
            
            # Save to dataframe
            row = pd.DataFrame([dict(subid = subid, 
                                     roi = mask + '-' + contrast, 
                                     mask_vox = num_voxels, 
                                     hemi = hemi,
                                     cond = label, 
                                     value = func_masked.mean()), ])
            df = df.append(row, ignore_index = True)


(501,)
(1942,)
(2443,)
(19293,)
(501,)
(1942,)
(2443,)
(19293,)
(501,)
(1942,)
(2443,)
(19293,)
(501,)
(1942,)
(2443,)
(19293,)
(501,)
(1942,)
(2443,)
(19293,)
(501,)
(1942,)
(2443,)
(19293,)
(501,)
(1942,)
(2443,)
(19293,)
(501,)
(1942,)
(2443,)
(19293,)
(501,)
(1942,)
(2443,)
(19293,)
(501,)
(1942,)
(2443,)
(19293,)
(501,)
(1942,)
(2443,)
(19293,)
(501,)
(1942,)
(2443,)
(19293,)
(501,)
(1942,)
(2443,)
(19293,)
(501,)
(1942,)
(2443,)
(19293,)
(501,)
(1942,)
(2443,)
(19293,)
(501,)
(1942,)
(2443,)
(19293,)
(501,)
(1942,)
(2443,)
(19293,)
(501,)
(1942,)
(2443,)
(19293,)
(501,)
(1942,)
(2443,)
(19293,)
(501,)
(1942,)
(2443,)
(19293,)
(501,)
(1942,)
(2443,)
(19293,)
(501,)
(1942,)
(2443,)
(19293,)
(501,)
(1942,)
(2443,)
(19293,)
(501,)
(1942,)
(2443,)
(19293,)
(501,)
(1942,)
(2443,)
(19293,)
(501,)
(1942,)
(2443,)
(19293,)
(501,)
(1942,)
(2443,)
(19293,)
(501,)
(1942,)
(2443,)
(19293,)
(501,)
(1942,)
(2443,)
(19293,)
(501,)
(1942,)
(2443,)
(19293,)
(501,)
(1942,)
(2443,)
(19293,)
(501,)
(1942,)
(2443,)
(19293,)
(501,)
(1942,)
(2443,)
(19293,)
(501,)
(1942,)
(2443,)
(19293,)
(501,)
(1942,)
(2443,)
(19293,)
(501,)
(1942,)
(2443,)
(19293,)
(501,)
(1942,)
(2443,)
(19293,)
(501,)
(1942,)
(2443,)
(19293,)
(501,)
(1942,)
(2443,)
(19293,)
(501,)
(1942,)
(2443,)
(19293,)
(501,)
(1942,)
(2443,)
(19293,)
(501,)
(1942,)
(2443,)
(19293,)
(501,)
(1942,)
(2443,)
(19293,)
(501,)
(1942,)
(2443,)
(19293,)
(501,)
(1942,)
(2443,)
(19293,)
(501,)
(1942,)
(2443,)
(19293,)
(501,)
(1942,)
(2443,)
(19293,)
(501,)
(1942,)
(2443,)
(19293,)
(501,)
(1942,)
(2443,)
(19293,)
(501,)
(1942,)
(2443,)
(19293,)
(501,)
(1942,)
(2443,)
(19293,)
(501,)
(1942,)
(2443,)
(19293,)
(501,)
(1942,)
(2443,)
(19293,)
(501,)
(1942,)
(2443,)
(19293,)
(501,)
(1942,)
(2443,)
(19293,)
(501,)
(1942,)
(2443,)
(19293,)
(501,)
(1942,)
(2443,)
(19293,)
(501,)
(1942,)
(2443,)
(19293,)
(501,)
(1942,)
(2443,)
(19293,)
(501,)
(1942,)
(2443,)
(19293,)
(501,)
(1942,)
(2443,)
(19293,)
(501,)
(1942,)
(2443,)
(19293,)
(501,)
(1942,)
(2443,)
(19293,)
(501,)
(1942,)
(2443,)
(19293,)
(501,)
(1942,)
(2443,)
(19293,)
(501,)
(1942,)
(2443,)
(19293,)
(501,)
(1942,)
(2443,)
(19293,)
(501,)
(1942,)
(2443,)
(19293,)
(501,)
(1942,)
(2443,)
(19293,)
(501,)
(1942,)
(2443,)
(19293,)
(501,)
(1942,)
(2443,)
(19293,)
(501,)
(1942,)
(2443,)
(19293,)
(501,)
(1942,)
(2443,)
(19293,)
(501,)
(1942,)
(2443,)
(19293,)
(501,)
(1942,)
(2443,)
(19293,)
(501,)
(1942,)
(2443,)
(19293,)
(501,)
(1942,)
(2443,)
(19293,)
(501,)
(1942,)
(2443,)
(19293,)
(501,)
(1942,)
(2443,)
(19293,)
(501,)
(1942,)
(2443,)
(19293,)
(501,)
(1942,)
(2443,)
(19293,)
(501,)
(1942,)
(2443,)
(19293,)
(501,)
(1942,)
(2443,)
(19293,)
(501,)
(1942,)
(2443,)
(19293,)
(501,)
(1942,)
(2443,)
(19293,)
(501,)
(1942,)
(2443,)
(19293,)
(501,)
(1942,)
(2443,)
(19293,)
(501,)
(1942,)
(2443,)
(19293,)
(501,)
(1942,)
(2443,)
(19293,)
(501,)
(1942,)
(2443,)
(19293,)
(501,)
(1942,)
(2443,)
(19293,)
(501,)
(1942,)
(2443,)
(19293,)
(501,)
(1942,)
(2443,)
(19293,)
(501,)
(1942,)
(2443,)
(19293,)
(501,)
(1942,)
(2443,)
(19293,)
(501,)
(1942,)
(2443,)
(19293,)
(501,)
(1942,)
(2443,)
(19293,)
(501,)
(1942,)
(2443,)
(19293,)
(501,)
(1942,)
(2443,)
(19293,)
(501,)
(1942,)
(2443,)
(19293,)
(501,)
(1942,)
(2443,)
(19293,)
(501,)
(1942,)
(2443,)
(19293,)
(501,)
(1942,)
(2443,)
(19293,)
(501,)
(1942,)
(2443,)
(19293,)
(501,)
(1942,)
(2443,)
(19293,)
(501,)
(1942,)
(2443,)
(19293,)
(501,)
(1942,)
(2443,)
(19293,)
(501,)
(1942,)
(2443,)
(19293,)
(501,)
(1942,)
(2443,)
(19293,)
(501,)
(1942,)
(2443,)
(19293,)
(501,)
(1942,)
(2443,)
(19293,)
(501,)
(1942,)
(2443,)
(19293,)
(501,)
(1942,)
(2443,)
(19293,)
(501,)
(1942,)
(2443,)
(19293,)
(501,)
(1942,)
(2443,)
(19293,)
(501,)
(1942,)
(2443,)
(19293,)
(501,)
(1942,)
(2443,)
(19293,)
(501,)
(1942,)
(2443,)
(19293,)
(501,)
(1942,)
(2443,)
(19293,)
(501,)
(1942,)
(2443,)
(19293,)
(501,)
(1942,)
(2443,)
(19293,)
(501,)
(1942,)
(2443,)
(19293,)
(501,)
(1942,)
(2443,)
(19293,)
(501,)
(1942,)
(2443,)
(19293,)
(501,)
(1942,)
(2443,)
(19293,)
(501,)
(1942,)
(2443,)
(19293,)

Vizualize data

Distribution of voxels per mask


In [335]:
means = pd.pivot_table(df,'mask_vox','roi')
print means


roi
hit-miss_thresh-anat                        19293
topdownALEventral_fslMNI_thresh-anat          501
topdownALEventral_inv_fslMNI_thresh-anat     1942
topdown_ALE_pN05_fslMNI_thresh-anat          2443
Name: mask_vox, dtype: float64

In [336]:
sns.factorplot(x='cond', y='value', col='roi', 
               aspect=1, kind='bar', col_wrap=4,
               dodge=0.01, ci=68, palette='husl',
               units='subid', data=df)


Out[336]:
<seaborn.axisgrid.FacetGrid at 0x161aa8210>

Reorganize


In [337]:
df_subset = df[(df.roi == 'topdownALEventral_inv_fslMNI_thresh-anat')&(df.cond !='Test')]
df_subset = df_subset.reset_index()
df_subset['beta'] = df_subset.value
df_subset.head()


Out[337]:
index cond hemi mask_vox roi subid value beta
0 5 1_old bilat 1942 topdownALEventral_inv_fslMNI_thresh-anat subj03 943.599243 943.599243
1 9 1_new bilat 1942 topdownALEventral_inv_fslMNI_thresh-anat subj03 1339.005127 1339.005127
2 13 2_old bilat 1942 topdownALEventral_inv_fslMNI_thresh-anat subj03 1187.835327 1187.835327
3 17 2_new bilat 1942 topdownALEventral_inv_fslMNI_thresh-anat subj03 880.185059 880.185059
4 21 3_old bilat 1942 topdownALEventral_inv_fslMNI_thresh-anat subj03 924.044373 924.044373

In [338]:
df_subset = df_subset[df_subset.mask_vox > 10].reset_index()

In [339]:
tmp = pd.DataFrame(df_subset.cond.str.split('_').tolist(), columns=['Morph', 'Response'])
df_subset = df_subset.join(tmp)
df_subset.head()


Out[339]:
level_0 index cond hemi mask_vox roi subid value beta Morph Response
0 0 5 1_old bilat 1942 topdownALEventral_inv_fslMNI_thresh-anat subj03 943.599243 943.599243 1 old
1 1 9 1_new bilat 1942 topdownALEventral_inv_fslMNI_thresh-anat subj03 1339.005127 1339.005127 1 new
2 2 13 2_old bilat 1942 topdownALEventral_inv_fslMNI_thresh-anat subj03 1187.835327 1187.835327 2 old
3 3 17 2_new bilat 1942 topdownALEventral_inv_fslMNI_thresh-anat subj03 880.185059 880.185059 2 new
4 4 21 3_old bilat 1942 topdownALEventral_inv_fslMNI_thresh-anat subj03 924.044373 924.044373 3 old

In [340]:
sns.factorplot(x='Morph', y='beta', #col='hemi', 
               units='subid', ci=68, kind='point',
               dodge=0.1, hue_order=['old', 'new'],
               hue='Response', data=df_subset, 
               palette=['dodgerblue', 'mediumseagreen'])

# plt.savefig('/Users/steph-backup/Dropbox/Experiments/ObjFam/submem/fusiform_bilat.png')


Out[340]:
<seaborn.axisgrid.FacetGrid at 0x16ab5a190>

In [305]:
sns.factorplot(x='Morph', y='beta', col='subid', col_wrap=5,
               ci=68, kind='point',
               dodge=0.1, hue_order=['old', 'new'],
               hue='Response', data=df_subset, 
               palette=['dodgerblue', 'mediumseagreen'])


Out[305]:
<seaborn.axisgrid.FacetGrid at 0x158e12ad0>

In [305]:


In [341]:
%R -i df_subset

In [343]:
%%R
df_subset$Morphq = as.numeric(df_subset$Morph)
print(str(df_subset))

contrasts(df_subset$Response) = cbind(OldvNew=c(-1, 1))
print(contrasts(df_subset$Response))

#contrasts(df_subset$hemi) = cbind(LvR=c(1, -1))
#print(contrasts(df_subset$hemi))

#res1 = lmer(beta~(scale(Morphq)*Response) + hemi + (1|subid), data=df_subset)
#print(summary(res1))

res1 = lmer(beta~(scale(Morphq)*Response) + (1 + Response + scale(Morphq)|subid), data=df_subset)
print(summary(res1))


'data.frame':	108 obs. of  12 variables:
 $ level_0 : int [1:108(1d)] 0 1 2 3 4 5 6 7 8 9 ...
 $ index   : int [1:108(1d)] 5 9 13 17 21 25 33 37 41 45 ...
 $ cond    : Factor w/ 6 levels "1_new","1_old",..: 2 1 4 3 6 5 2 1 4 3 ...
 $ hemi    : Factor w/ 1 level "bilat": 1 1 1 1 1 1 1 1 1 1 ...
 $ mask_vox: num [1:108(1d)] 1942 1942 1942 1942 1942 ...
 $ roi     : Factor w/ 1 level "topdownALEventral_inv_fslMNI_thresh-anat": 1 1 1 1 1 1 1 1 1 1 ...
 $ subid   : Factor w/ 18 levels "subj03","subj04",..: 1 1 1 1 1 1 2 2 2 2 ...
 $ value   : num [1:108(1d)] 944 1339 1188 880 924 ...
 $ beta    : num [1:108(1d)] 944 1339 1188 880 924 ...
 $ Morph   : Factor w/ 3 levels "1","2","3": 1 1 2 2 3 3 1 1 2 2 ...
 $ Response: Factor w/ 2 levels "new","old": 2 1 2 1 2 1 2 1 2 1 ...
  ..- attr(*, "contrasts")= num [1:2, 1] -1 1
  .. ..- attr(*, "dimnames")=List of 2
  .. .. ..$ : chr  "new" "old"
  .. .. ..$ : chr "OldvNew"
 $ Morphq  : num  1 1 2 2 3 3 1 1 2 2 ...
NULL
    OldvNew
new      -1
old       1
Linear mixed model fit by REML ['merModLmerTest']
Formula: beta ~ (scale(Morphq) * Response) + (1 + Response + scale(Morphq) |  
    subid)
   Data: df_subset

REML criterion at convergence: 1435.3

Scaled residuals: 
     Min       1Q   Median       3Q      Max 
-2.27455 -0.63194 -0.04453  0.70869  1.87359 

Random effects:
 Groups   Name            Variance Std.Dev. Corr     
 subid    (Intercept)     122594   350.13            
          ResponseOldvNew   4309    65.64   0.24     
          scale(Morphq)     1584    39.80   0.45 0.98
 Residual                  23808   154.30            
Number of obs: 108, groups: subid, 18

Fixed effects:
                              Estimate Std. Error      df t value Pr(>|t|)    
(Intercept)                    524.682     83.852  17.000   6.257 8.68e-06 ***
scale(Morphq)                   -6.938     17.621  24.610  -0.394    0.697    
ResponseOldvNew                 18.626     21.444  17.830   0.869    0.397    
scale(Morphq):ResponseOldvNew  -16.456     14.917  70.000  -1.103    0.274    
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Correlation of Fixed Effects:
            (Intr) scl(M) RspnON
scal(Mrphq) 0.234               
RspnsOldvNw 0.169  0.375        
scl(Mr):RON 0.000  0.000  0.000 

In [282]:
%%R
## Summarizes data.
## Gives count, mean, standard deviation, standard error of the mean, and confidence interval (default 95%).
##   data: a data frame.
##   measurevar: the name of a column that contains the variable to be summariezed
##   groupvars: a vector containing names of columns that contain grouping variables
##   na.rm: a boolean that indicates whether to ignore NA's
##   conf.interval: the percent range of the confidence interval (default is 95%)
summarySE <- function(data=NULL, measurevar, groupvars=NULL, na.rm=FALSE,
                      conf.interval=.95, .drop=TRUE) {
    require(plyr)

    # New version of length which can handle NA's: if na.rm==T, don't count them
    length2 <- function (x, na.rm=FALSE) {
        if (na.rm) sum(!is.na(x))
        else       length(x)
    }

    # This does the summary. For each group's data frame, return a vector with
    # N, mean, and sd
    datac <- ddply(data, groupvars, .drop=.drop,
      .fun = function(xx, col) {
        c(N    = length2(xx[[col]], na.rm=na.rm),
          mean = mean   (xx[[col]], na.rm=na.rm),
          sd   = sd     (xx[[col]], na.rm=na.rm)
        )
      },
      measurevar
    )

    # Rename the "mean" column    
    datac <- rename(datac, c("mean" = measurevar))

    datac$se <- datac$sd / sqrt(datac$N)  # Calculate standard error of the mean

    # Confidence interval multiplier for standard error
    # Calculate t-statistic for confidence interval: 
    # e.g., if conf.interval is .95, use .975 (above/below), and use df=N-1
    ciMult <- qt(conf.interval/2 + .5, datac$N-1)
    datac$ci <- datac$se * ciMult

    return(datac)
}
## Norms the data within specified groups in a data frame; it normalizes each
## subject (identified by idvar) so that they have the same mean, within each group
## specified by betweenvars.
##   data: a data frame.
##   idvar: the name of a column that identifies each subject (or matched subjects)
##   measurevar: the name of a column that contains the variable to be summariezed
##   betweenvars: a vector containing names of columns that are between-subjects variables
##   na.rm: a boolean that indicates whether to ignore NA's
normDataWithin <- function(data=NULL, idvar, measurevar, betweenvars=NULL,
                           na.rm=FALSE, .drop=TRUE) {
    require(plyr)

    # Measure var on left, idvar + between vars on right of formula.
    data.subjMean <- ddply(data, c(idvar, betweenvars), .drop=.drop,
     .fun = function(xx, col, na.rm) {
        c(subjMean = mean(xx[,col], na.rm=na.rm))
      },
      measurevar,
      na.rm
    )

    # Put the subject means with original data
    data <- merge(data, data.subjMean)

    # Get the normalized data in a new column
    measureNormedVar <- paste(measurevar, "_norm", sep="")
    data[,measureNormedVar] <- data[,measurevar] - data[,"subjMean"] +
                               mean(data[,measurevar], na.rm=na.rm)

    # Remove this subject mean column
    data$subjMean <- NULL

    return(data)
}
## Summarizes data, handling within-subjects variables by removing inter-subject variability.
## It will still work if there are no within-S variables.
## Gives count, un-normed mean, normed mean (with same between-group mean),
##   standard deviation, standard error of the mean, and confidence interval.
## If there are within-subject variables, calculate adjusted values using method from Morey (2008).
##   data: a data frame.
##   measurevar: the name of a column that contains the variable to be summariezed
##   betweenvars: a vector containing names of columns that are between-subjects variables
##   withinvars: a vector containing names of columns that are within-subjects variables
##   idvar: the name of a column that identifies each subject (or matched subjects)
##   na.rm: a boolean that indicates whether to ignore NA's
##   conf.interval: the percent range of the confidence interval (default is 95%)
summarySEwithin <- function(data=NULL, measurevar, betweenvars=NULL, withinvars=NULL,
                            idvar=NULL, na.rm=FALSE, conf.interval=.95, .drop=TRUE) {

  # Ensure that the betweenvars and withinvars are factors
  factorvars <- vapply(data[, c(betweenvars, withinvars), drop=FALSE],
    FUN=is.factor, FUN.VALUE=logical(1))

  if (!all(factorvars)) {
    nonfactorvars <- names(factorvars)[!factorvars]
    message("Automatically converting the following non-factors to factors: ",
            paste(nonfactorvars, collapse = ", "))
    data[nonfactorvars] <- lapply(data[nonfactorvars], factor)
  }

  # Get the means from the un-normed data
  datac <- summarySE(data, measurevar, groupvars=c(betweenvars, withinvars),
                     na.rm=na.rm, conf.interval=conf.interval, .drop=.drop)

  # Drop all the unused columns (these will be calculated with normed data)
  datac$sd <- NULL
  datac$se <- NULL
  datac$ci <- NULL

  # Norm each subject's data
  ndata <- normDataWithin(data, idvar, measurevar, betweenvars, na.rm, .drop=.drop)

  # This is the name of the new column
  measurevar_n <- paste(measurevar, "_norm", sep="")

  # Collapse the normed data - now we can treat between and within vars the same
  ndatac <- summarySE(ndata, measurevar_n, groupvars=c(betweenvars, withinvars),
                      na.rm=na.rm, conf.interval=conf.interval, .drop=.drop)

  # Apply correction from Morey (2008) to the standard error and confidence interval
  #  Get the product of the number of conditions of within-S variables
  nWithinGroups    <- prod(vapply(ndatac[,withinvars, drop=FALSE], FUN=nlevels,
                           FUN.VALUE=numeric(1)))
  correctionFactor <- sqrt( nWithinGroups / (nWithinGroups-1) )

  # Apply the correction factor
  ndatac$sd <- ndatac$sd * correctionFactor
  ndatac$se <- ndatac$se * correctionFactor
  ndatac$ci <- ndatac$ci * correctionFactor

  # Combine the un-normed means with the normed results
  merge(datac, ndatac)
}

In [344]:
%%R
library(ggplot2)

# change background to white, increase font size
theme_set(theme_bw(base_size = 22)) 

# set up study parameters
y = 'beta'
x = 'Morph'
z = 'Response'

color_palette = c('mediumseagreen', 'dodgerblue')
pos_dodge = position_dodge(.13)
xlabel = 'Morph'
ylabel = 'beta'
legend_title= 'Response'

# for between subjects variables
data_summary <- summarySEwithin(df_subset, 
                                  measurevar=y, 
                                  withinvars=c(z,x),
                               idvar='subid')

# set color palette
ggplot(data_summary, 
       aes(x = Morph, y = beta, 
           colour = Response, group = Response)) + 
  geom_errorbar(aes(ymin = beta-se, ymax = beta+se), 
                width=0, position=pos_dodge,
                size = 2) +
  geom_line(position=pos_dodge, size=2) +
  geom_point(position=pos_dodge, size=8) +
  xlab(xlabel) + 
  ylab(ylabel) +
  scale_color_manual(values = color_palette,
                     name = legend_title)

ggsave(file="~/Dropbox/Experiments/ObjFam/betas_topdownALEventral_inv_fslMNI_thresh-anat.png", width=7, height=4)


ymax not defined: adjusting position using y instead
ymax not defined: adjusting position using y instead

Relabel Conditions Based on Research Question


In [148]:
df['condition'] = df['cond']
cond_names = {'1_old': 'specificRecog', '2_old': 'falseRecog', '3_old': 'falseRecog', 
              '1_new': 'miss', '2_new': 'cr', '3_new': 'cr'}

for condition in cond_names.keys():
    df.loc[df.cond.isin([condition]), 'condition'] = cond_names[condition]

In [22]:
df.head()


Out[22]:
cond hemi mask_vox roi subid value condition
0 Study lh 539 Left-Cerebral-Cortex-rev_linear-study subj03 -638.278076 Study
1 Study rh 445 Right-Cerebral-Cortex-rev_linear-study subj03 -771.010559 Study
2 Study lh 96 Left-Cerebral-Cortex-linear-study subj03 1346.173950 Study
3 Study rh 139 Right-Cerebral-Cortex-linear-study subj03 2445.500732 Study
4 Study lh 85 Left-Hippocampus-anat subj03 -123.579437 Study

In [23]:
sns.factorplot(x='condition', y='value', col='roi', 
               aspect=1.2, kind='bar', col_wrap=3,
               dodge=0.01, ci=68, palette='husl',
               units='subid', data=df)


Out[23]:
<seaborn.axisgrid.FacetGrid at 0x11bc16210>

Stats


In [24]:
%R -i df

In [27]:
%%R
roi_name = 'Left-Hippocampus-anat'
cond_list = c('falseRecog', 'specificRecog')

data = df[df$condition %in% cond_list, ]
data = data[data$roi == roi_name, ]

# Factor categorical variables
data$roi = factor(data$roi)
data$condition = factor(data$condition)
print(str(data))

contrasts(data$condition) = c(-1,1)
print(contrasts(data$condition))

# Mixed-effects model
rs1 = lmer(value~condition + (1|subid), data = data)
rs2 = lmer(value~condition + (1 + condition|subid), data = data)

print(anova(rs1, rs2))


'data.frame':	54 obs. of  7 variables:
 $ cond     : Factor w/ 7 levels "1_new","1_old",..: 2 4 6 2 4 6 2 4 6 2 ...
 $ hemi     : Factor w/ 2 levels "lh","rh": 1 1 1 1 1 1 1 1 1 1 ...
 $ mask_vox : num [1:54(1d)] 85 85 85 79 79 79 106 106 106 57 ...
 $ roi      : Factor w/ 1 level "Left-Hippocampus-anat": 1 1 1 1 1 1 1 1 1 1 ...
 $ subid    : Factor w/ 18 levels "subj03","subj04",..: 1 1 1 2 2 2 3 3 3 4 ...
 $ value    : num [1:54(1d)] -222 -532 -123 -184 -150 ...
 $ condition: Factor w/ 2 levels "falseRecog","specificRecog": 2 1 1 2 1 1 2 1 1 2 ...
NULL
              [,1]
falseRecog      -1
specificRecog    1
refitting model(s) with ML (instead of REML)
Data: data
Models:
object: value ~ condition + (1 | subid)
..1: value ~ condition + (1 + condition | subid)
       Df    AIC    BIC  logLik deviance  Chisq Chi Df Pr(>Chisq)
object  4 790.90 798.85 -391.45   782.90                         
..1     6 791.98 803.92 -389.99   779.98 2.9167      2     0.2326

In [28]:
%%R
print(summary(rs1))


Linear mixed model fit by REML ['merModLmerTest']
Formula: value ~ condition + (1 | subid)
   Data: data

REML criterion at convergence: 763.3

Scaled residuals: 
     Min       1Q   Median       3Q      Max 
-1.74131 -0.68726  0.04178  0.48919  2.33175 

Random effects:
 Groups   Name        Variance Std.Dev.
 subid    (Intercept) 78224    279.7   
 Residual             75016    273.9   
Number of obs: 54, groups: subid, 18

Fixed effects:
            Estimate Std. Error      df t value Pr(>|t|)   
(Intercept)  -295.47      76.87   18.04  -3.844  0.00119 **
condition1     52.47      39.53   35.00   1.327  0.19301   
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Correlation of Fixed Effects:
           (Intr)
condition1 0.171 

In [ ]: