def get_broad_sense_heritability(self, pids=None): """ Estimates the broad sense heritability from replicates. """ import linear_models as lm if not pids: pids = sorted(self.phen_dict.keys()) bs_herits = [] bs_avg_herits = [] bs_pids = [] bs_herit_pvals = [] for pid in pids: ets = map(int, self.get_ecotypes(pid)) num_vals = len(ets) num_ets = len(set(ets)) if num_vals == num_ets: print "Can't estimate the broad sense heritability when replicates are missing." continue else: avg_repl_num = float(num_vals) / num_ets print 'Average number of replicates:', avg_repl_num values = self.get_values(pid) mod = lm.LinearModel(values) res = mod.anova_f_test([sp.array(ets)]) bs_herit = res['var_perc'][0] bs_herit_pval = res['ps'][0] bs_herit_pvals.append(bs_herit_pval) print 'Heritability:', bs_herit print 'Heritability (different from 0) p-value :', bs_herit_pval bs_avg_herit = 1.0 - (1.0 - bs_herit) / avg_repl_num bs_avg_herits.append(bs_avg_herit) print 'Estimated mean value heritability:', bs_avg_herit bs_herits.append(bs_herit) bs_pids.append(pid) return {'bs_herits':bs_herits, 'bs_pids':bs_pids, 'bs_avg_herits':bs_avg_herits, 'bs_herit_pvals':bs_herit_pvals}
def get_broad_sense_heritability(self): """ Estimates the broad sense heritability from replicates. """ import linear_models as lm ets = map(int, self.ecotypes) num_vals = len(ets) num_ets = len(set(ets)) if num_vals == num_ets: log.warning( "Can't estimate the broad sense heritability when replicates are missing." ) else: avg_repl_num = float(num_vals) / num_ets log.info('Average number of replicates:%s' % avg_repl_num) values = self.values mod = lm.LinearModel(values) res = mod.anova_f_test([sp.array(ets)]) bs_herit = res['var_perc'][0] bs_herit_pval = res['ps'][0] log.info('Heritability:%s' % bs_herit) log.info('Heritability (different from 0) p-value :%s' % bs_herit_pval) bs_avg_herit = 1.0 - (1.0 - bs_herit) / avg_repl_num log.info('Estimated mean value heritability:%s' % bs_avg_herit) return { 'bs_herit': bs_herit, 'bs_pid': bs_pid, 'bs_avg_herit': bs_avg_herit, 'bs_herit_pval': bs_herit_pval }
def get_blup(self, pid, K): """ Returns the REML estimate for the BLUP and the pseudo-heritability. """ from scipy import stats import linear_models as lm phen_vals = self.get_values(pid) lmm = lm.LinearMixedModel(phen_vals) if len(phen_vals) == len(set(phen_vals)): lmm.add_random_effect(K) else: Z = self.get_incidence_matrix(pid) lmm.add_random_effect(Z * K * Z.T) r1 = lmm.get_REML() ll1 = r1['max_ll'] rlm = lm.LinearModel(phen_vals) ll0 = rlm.get_ll() lrt_stat = 2 * (ll1 - ll0) pval = stats.chi2.sf(lrt_stat, 1) #Now the BLUP. y_mean = sp.mean(lmm.Y) Y = lmm.Y - y_mean p_herit = r1['pseudo_heritability'] delta = (1 - p_herit) / p_herit # if K_inverse == None: # K_inverse = K.I # M = (sp.eye(K.shape[0]) + delta * K_inverse) # u_blup = M.I * Y M = (K + delta * sp.eye(K.shape[0])) u_mean_pred = K * (M.I * Y) blup_residuals = Y - u_mean_pred return {'pseudo_heritability':r1['pseudo_heritability'], 'pval':pval, 'u_blup':u_mean_pred, 'blup_residuals':blup_residuals}
def get_pseudo_heritability(self, K): """ Returns the REML estimate of the heritability. methods: 'avg' (averages), 'repl' (replicates) """ from scipy import stats import linear_models as lm lmm = lm.LinearMixedModel(self.phen_vals) if len(self.values) == len(set(self.values)): lmm.add_random_effect(K) else: Z = self.get_incidence_matrix() lmm.add_random_effect(Z * K * Z.T) r1 = lmm.get_REML() ll1 = r1['max_ll'] rlm = lm.LinearModel(self.values) ll0 = rlm.get_ll() lrt_stat = 2 * (ll1 - ll0) pval = stats.chi2.sf(lrt_stat, 1) return {'pseudo_heritability': r1['pseudo_heritability'], 'pval': pval}