def test_significant(): # Some test data and test code taken from example at: # http://www.jarrodmillman.com/rcsds/lectures/glm_intro.html psychopathy = [11.416, 4.514, 12.204, 14.835, 8.416, 6.563, 17.343, 13.02, 15.19 , 11.902, 22.721, 22.324] clammy = [0.389, 0.2 , 0.241, 0.463, 4.585, 1.097, 1.642, 4.972, 7.957, 5.585, 5.527, 6.964] def t_stat(y, X, c): """ betas, t statistic and significance test given data, design matrix, contrast This is OLS estimation; we assume the errors to have independent and identical normal distributions around zero for each $i$ in $\e_i$ (i.i.d). """ # Make sure y, X, c are all arrays y = np.asarray(y) X = np.asarray(X) c = np.atleast_2d(c).T # As column vector # Calculate the parameters - b hat beta = npl.pinv(X).dot(y) # The fitted values - y hat fitted = X.dot(beta) # Residual error errors = y - fitted # Residual sum of squares RSS = (errors**2).sum(axis=0) # Degrees of freedom is the number of observations n minus the number # of independent regressors we have used. If all the regressor # columns in X are independent then the (matrix rank of X) == p # (where p the number of columns in X). If there is one column that # can be expressed as a linear sum of the other columns then # (matrix rank of X) will be p - 1 - and so on. df = X.shape[0] - npl.matrix_rank(X) # Mean residual sum of squares MRSS = RSS / df # calculate bottom half of t statistic SE = np.sqrt(MRSS * c.T.dot(npl.pinv(X.T.dot(X)).dot(c))) t = c.T.dot(beta) / SE # Get p value for t value using cumulative density dunction # (CDF) of t distribution ltp = t_dist.cdf(t, df) # lower tail p p = 1 - ltp # upper tail p return beta, t, df, p X = np.column_stack((np.ones(12), clammy)) Y = np.asarray(psychopathy) B1, t1, df1, p1 = t_stat(Y, X, [1, 0]) B2, t2, df2, p2 = t_stat(Y, X, [0, 1]) #---------------------------------------------------------# # my function myt1, myt2 = significant(X, Y, B1) # assert assert_almost_equal(t1.ravel(), myt1) assert_almost_equal(t2.ravel(), myt2)
boldname='ds005/sub0'+str(i).zfill(2)+'/model/model001/task001_run00'+`j`+'.feat/filtered_func_data_mni.nii.gz' img=nib.load(boldname) data=img.get_data() data=smooth_spatial(data) run = j behav_cond = 'ds005/sub0'+str(i).zfill(2)+'/behav/task001_run00'+`j`+'/behavdata.txt' task_cond1 = 'ds005/sub0'+str(i).zfill(2)+'/model/model001/onsets/task001_run00'+`j`+'/cond001.txt' task_cond2 = 'ds005/sub0'+str(i).zfill(2)+'/model/model001/onsets/task001_run00'+`j`+'/cond002.txt' task_cond3 = 'ds005/sub0'+str(i).zfill(2)+'/model/model001/onsets/task001_run00'+`j`+'/cond003.txt' task_cond4 = 'ds005/sub0'+str(i).zfill(2)+'/model/model001/onsets/task001_run00'+`j`+'/cond004.txt' parameters = merge_cond(behav_cond, task_cond1, task_cond2, task_cond3, task_cond4) neural_prediction = events2neural_extend(parameters,TR, n_vols) gain, loss, linear_dr, quad_dr = getRegressor(TR, n_vols, hrf_at_trs, neural_prediction, standard = True) data, gain, loss, linear_dr, quad_dr = deleteOutliers(data, gain, loss, i, run, dvars_out, fd_out) data_full = np.concatenate((data_full,data),axis=3) gain_full = np.concatenate((gain_full,gain),axis=0) loss_full = np.concatenate((loss_full,loss),axis=0) # mea=calcMRSS(data_full, gain_full, loss_full, None, None, threshold) X, Y, beta=calcBeta(data_full, gain_full, loss_full, None, None, threshold) # calculate t values t_val=np.zeros((2,902629)) for k in range(Y.shape[1]): t_val[:,k] = significant(X,Y[:,k], beta[:,k]) # file names for beta and t beta_file='../results/texts/sub0'+str(i).zfill(2)+'_standard_beta.txt' t_file='../results/texts/sub0'+str(i).zfill(2)+'_standard_tvals.txt' # save beta and t values to file np.savetxt(beta_file, beta) np.savetxt(t_file, t_val)