# coding = UTF-8 # =================================== # 这个程序用来实现Regression-Adjustment # =================================== import numpy as np from libs.imexport.class_statadata import Statadata from libs.statmodel.class_lm import Lm from rpy2.robjects.packages import importr from libs.embeddedR.class_renv import REnv # 1. 导入数据 stata_file = r'd:\data\userdata\jtrain2.dta' stdata = Statadata(stata_file) raw_data = stdata.read() # 2. 比较两组对象 grouped = raw_data.groupby('train') compared = grouped.aggregate(np.mean) print(compared.T) for _, group in grouped: print(group.mean()) # 3. 回归 reg_obj = Lm(data=raw_data, formula='re78 ~ train') call_obj = reg_obj() print(reg_obj.coefs) reg_obj = Lm(
bootBias = pd.DataFrame(boot_data-original).mean() pdict = OrderedDict() pdict['original'] = original pdict['bootSE'] = bootSE pdict['bootBias'] = bootBias return {'boot_data':boot_data, 'stat':pd.DataFrame(pdict)} if __name__ == '__main__': ''' stata_file = 'd:/data/userdata/newcity.dta' stdata = Statadata(stata_file) city_data = stdata.read() boot = Bootstrap(data=city_data, func='function(d, w) sum(d$x * w)/sum(d$u * w)') result = boot.run(stype='"w"') print(result['stat']) ''' stata_file = 'd:/data/userdata/mtcars.dta' stdata = Statadata(stata_file) cars_data = stdata.read() boot = Bootstrap(data=cars_data, func='function(formula, data, indices){\n\td <- data[indices,]\n\tfit <- lm(formula, data=d)\n\treturn(coef(fit))}') result = boot.run(formula='mpg~wt+disp') print(result['stat'])
def adj_Rsquared(self): return self.robj.get_var('slm_obj$adj.r.squared') @property def cov(self): return self.robj.get_var('slm_obj$cov.unscaled') @property def qr(self): return self.robj.get_var('lm_obj$qr$qr') if __name__ == '__main__': rthread = RThread() stata_file = r'D:\data\test\wage1.dta' stdata = Statadata(stata_file) mdata = stdata.read() rdata = mdata[['lwage','educ','exper','tenure']] rthread.create_vars(rdata) print(rthread.get_var('lwage')) #lm = Rreg(robj=rthread,xvar=['educ','exper','tenure'],yvar='lwage') ''' print(lm.coefficients) print(lm.fstatistic) print(lm.sigma) print(lm.df) print(lm.qr) print(lm.Rsquared) print(lm.adj_Rsquared)
# coding = UTF-8 # =================================== # 这个程序用来实现Regression-Adjustment # =================================== import numpy as np from libs.imexport.class_statadata import Statadata from libs.statmodel.class_lm import Lm from rpy2.robjects.packages import importr from libs.embeddedR.class_renv import REnv # 1. 导入数据 stata_file = r'd:\data\userdata\jtrain2.dta' stdata = Statadata(stata_file) raw_data = stdata.read() # 2. 比较两组对象 grouped = raw_data.groupby('train') compared = grouped.aggregate(np.mean) print(compared.T) for _, group in grouped: print(group.mean()) # 3. 回归 reg_obj = Lm(data=raw_data, formula = 're78 ~ train') call_obj = reg_obj() print(reg_obj.coefs) reg_obj = Lm(data=raw_data, formula = 're78 ~ train + re74 + re75 + age + agesq + nodegree + married + black + hisp')