简体   繁体   中英

Multiple regression using OLS from stats model

I have looked up similar questions to mine, I cannot find an answer.

My aim: I have survival data. I want the residuals for the survival data, after accounting for age and weight.

Method:

import statsmodels
from statsmodels import ols
species = ["sample1","sample2","sample3","sample4","sample5"]
Survival = [0.1,0.2,0.3,0.4,0.5]
Age = [1,2,3,4,5]
Weight = [1,3,5.5,7,10]
mymodel = ols.ols(Mortality,[Weight,Age],"Mortality",["Weight","Age"])
print mymodel

Output: My ideal output is a table with two columns, one column being the species, the other column being the mortality residual after I have accounted for age and weight.

Questions: 1.No matter what I do, I cannot find the ols method. I have statsmodel installed. When I open a python console, and do dir(statsmodels), there are the options that I get:

['CacheWriteWarning', 'ConvergenceWarning', 'InvalidTestWarning', 'IterationLimitWarning', 'NoseWrapper', 'Tester', '__builtins__', '__doc__', '__docformat__', '__file__', '__name__', '__package__', '__path__', '__version__', 'compat', 'datasets', 'distributions', 'errstate', 'print_function', 'simplefilter', 'test', 'tools', 'version']

Where is the ols method?

  1. Hopefully once I can actually find the method, I will be able to run the code and ask where I can find the residuals that I'm looking for.

Thanks

Thank you, that worked.

Here is the code that I used:

import numpy as np
import statsmodels.api as sm
import statsmodels.formula.api as smf
import sys

dat = np.loadtxt(sys.argv[1],dtype={"names":("Species","Weight","Mortality","Age"),"formats":("S20","f4","f4","f4")})
mymodel = smf.ols("Mortality~Weight+Age",data=dat).fit()
Residues = list(mymodel.resid_pearson)
for each_species,each_residue in zip(list(dat["Species"]),Residues):
    print each_species + "\t" + str(each_residue)

So I think/hope what is happening here is that I'm reading in the table, fitting a multiple regression to the data, and printing the species name and the residual for each species (the residual being the regression of mortality against weight and age). Thank you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM