def Bootstrapwrapper(self, chi): '''Wrapper method for ADF Chi2 bootstrap''' # Run boostrap s, gamma = self.BootstrapChiADF() s = pd.DataFrame(s, index = self.mod_semopy.vars['observed'],\ columns = self.mod_semopy.vars['observed']) # Get new results (no need to run inspector) # NOTE: in May 2021 semopy cannot handle custom weighting matrices for # its WLS procedure. Make sure you mod semopy such that it can handle them model = Model(self.equation, mimic_lavaan=self.mimic_lavaan, baseline=self.bool_baseline) est = model.fit(self.data, cov=s, obj=self.obj, solver=self.solver, custom_w=gamma) # Get Chi2_b, Corrected Chi2 and the chi2 bias chi_b = stats.calc_chi2(model)[0] chi_corr = 2 * chi - chi_b bias_chi2 = chi_b - chi return chi_b, chi_corr, bias_chi2
def sem(name, data): desc = ''' # measurement model # *** DOMAINS *** employment =~ number_of_jobs + income_from_jobs # + sex housing =~ housing_space + sanitation_of_housing # + relocation_to_bhasanchar education =~ quality_of_education + lt_12_education + between_12_18_education + gt_18_education health =~ psychological_healthcare + number_of_healthcare_facilities + quality_of_healthcare social_links =~ help_from_ngo + trust_on_ngo + trust_on_law_enforcement social_bridges =~ possibility_of_friendship_with_host + friendship_with_host + marriage_to_host social_bonds =~ bond_with_rohingyas_outside + bond_with_neighbors + bond_with_majhis # + number_of_friends + bond_with_imams language_and_cultural_knowledge =~ number_of_religious_facilities + removal_of_religious_barriers + cultural_mixability safety_and_stability =~ intention_to_leave + improvement_in_6mos + feeling_about_future + number_of_violence + fear_of_children_safety # + stress + knowledge_of_missing_children + number_of_sexual_harassment + fear_of_leaving_again + discussion_about_violent_groups rights_and_citizenship =~ repatriation_in_home + return_to_home + rights_in_home # *** THEMES *** markers_and_means =~ education + employment + housing + health social_connections =~ social_bonds + social_bridges + social_links facilitators =~ language_and_cultural_knowledge + safety_and_stability foundation =~ rights_and_citizenship integration =~ markers_and_means + social_connections + facilitators + foundation # regressions number_of_jobs ~ sex # should we use it? # residual correlations removal_of_religious_barriers ~~ number_of_religious_facilities ''' mod = Model(desc) res = mod.fit(data) ins = mod.inspect() # pprint(ins) ins.to_excel('../result/inspect_' + name + '.xlsx', index=False) stat = semopy.calc_stats(mod) stat = stat.T stat.to_excel('../result/stat_' + name + '.xlsx') g = semopy.semplot(mod, '../plots/' + name + '.pdf')
def semopyFit(self, dataframe): ''''Fits the model with semopy. Helper function. ''' # Set model model = Model(self.equation, mimic_lavaan=self.mimic_lavaan, baseline=self.bool_baseline) # Estimate model est = model.fit(dataframe, obj=self.obj, solver=self.solver) # Get results res = model.inspect(std_est=True, se_robust=self.bool_robust) return model, est, res
import semopy from semopy import Model import pandas as pd from pprint import pprint desc = semopy.examples.political_democracy.get_model() print(desc) data = semopy.examples.political_democracy.get_data() # print(type(data), data.columns) mod = Model(desc) res = mod.fit(data) # print(res) ins = mod.inspect() # pprint(ins)
logliks = np.zeros((n_subjects, n_models)) # likelihoods lmes = np.zeros((n_subjects, n_models)) # LMEs = -0.5 * BICs bics = np.zeros((n_subjects, n_models)) # BICs ks = np.zeros((n_subjects, n_models)) # # params ns = np.zeros((n_subjects, n_models)) # # data points for i in range(n_subjects): for j in range(n_models): model = Model(descs[j]) filepath = os.path.join(dirname, files[i]) data = pd.read_csv(filepath, sep='\t') opt_res = model.fit(data) # from /Users/momchil/anaconda3/lib/python3.7/site-packages/semopy/stats.py: calc_bic() # WARNING: all of these are up to a proportionality constant that DIFFERS ACROSS SUBJECTS => do not use for BMS # ''' logliks[i,j] = stats.calc_likelihood(model) # note up to proportionality constant, b/c w.r.t. saturated model, but that's the same for all models so it's fine ks[i,j], ns[i,j] = len(model.param_vals), model.mx_data.shape[0] bic = stats.calc_bic(model) lmes[i,j] = -0.5 * bic ''' # calculate likelihood manually, following https://en.wikipedia.org/wiki/Wishart_distribution # and https://math.stackexchange.com/questions/2803164/degrees-of-freedom-in-a-wishart-distribution # and /Users/momchil/anaconda3/lib/python3.7/site-packages/semopy/stats.py: obj_mlw() # and /Users/momchil/anaconda3/lib/python3.7/site-packages/semopy/stats.py: calc_bic()