擅长:python、mysql、java
<p>这段代码向您展示了如何返回在函数中计算的项,以及如何在调用函数中检索它们。注意,您可以返回任意多或任意少的项,即使函数返回调用代码不需要的项,您也可以忽略它们。(这就是名为<strong>dummy</strong>的变量的用途)</p>
<p>我使用的是statsmodels附带的一个数据集,因为我无法识别您使用的数据集。你知道吗</p>
<pre><code>import statsmodels as sm
df = sm.datasets.get_rdataset("Guerry", "HistData").data
df = df[['Lottery', 'Literacy', 'Wealth', 'Region']].dropna()
import statsmodels.formula.api as smf
def regress(variables):
results = [ ]
for variable in variables:
mod = smf.ols(formula='Lottery ~ %s' % variable, data=df)
result = mod.fit()
results . append ( (variable, result.params, result.df_resid, result.rsquared) )
return results
for result in regress (['Literacy', 'Wealth', 'Region']):
variable, dummy, dummy, R_squared = result
print ( variable, R_squared )
</code></pre>
<p>结果如下:</p>
<pre><code>Literacy 0.145720612937
Wealth 0.243180384656
Region 0.142107524677
</code></pre>