Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 线性回归残差-我是否应该“;“标准化”;结果以及如何做到这一点_Python_Scipy_Regression_Linear Regression_Statsmodels - Fatal编程技术网

Python 线性回归残差-我是否应该“;“标准化”;结果以及如何做到这一点

Python 线性回归残差-我是否应该“;“标准化”;结果以及如何做到这一点,python,scipy,regression,linear-regression,statsmodels,Python,Scipy,Regression,Linear Regression,Statsmodels,我是生物学家。我想复制我在一篇论文中读到的一种方法:“为了允许独立于体重调查与死亡率的关联,死亡率的残差是通过从观察值中减去预测值来计算的” 我有一组死亡率(范围从0.1到0.5),一组体重(范围从2到80),我想在考虑体重后计算死亡率的残差 我写了这段代码: import scipy from scipy import stats import sys # This reads in the weight and mortality data to two lists. Weight =

我是生物学家。我想复制我在一篇论文中读到的一种方法:“为了允许独立于体重调查与死亡率的关联,死亡率的残差是通过从观察值中减去预测值来计算的”

我有一组死亡率(范围从0.1到0.5),一组体重(范围从2到80),我想在考虑体重后计算死亡率的残差

我写了这段代码:

import scipy
from scipy import stats
import sys


# This reads in the weight and mortality data to two lists. 
Weight = []
Mortality = []
for line in open(sys.argv[1]):
        line = line.strip().split()
        Weight.append(float(line[-2]))
        Mortality.append(float(line[-1]))

# This calculates the regression equation.
slope, intercept, r_value, p_value, std_err = scipy.stats.linregress(Mortality,Weight)

# This calculates the predicted value for each observed value
obs_values = Mortality
pred_values = []
for i in obs_values:
    pred_i = float(i) * float(slope) + float(intercept)
    pred_values.append(pred_i)

# This prints the residual for each pair of observations
for obs_v,pred_v in zip(obs_values,pred_values):
    Residual = str(obs_v - pred_v)
    print Residual
我的问题是,当我运行这段代码时,我的一些残差看起来相当大:

我想知道,这些结果看起来是否“正常”/它们是否应该以某种方式“标准化”/在考虑体重后,我是否做了一些错误的事情来获得死亡率的残差

如果可能的话,我会很感激简单的“简单的英语”答案和可能的代码片段,因为我不是一个统计专家


非常感谢

查看文档:第一个参数是
x
,横坐标,第二个参数是
y
,您观察到的值。因此,如果观察值为
obs_values=死亡率
,则必须排列线性回归的两个参数,并根据
权重
作为
x
(而不是
死亡率
作为
y
)计算预测值:

另外,通过使用numpy,您可以大幅减少(并加速)代码(scipy仍然使用它)


我知道我不打算在这里提出后续问题,如果有人能告诉我如何在不点击“回答问题”的情况下继续讨论我的原始问题(带代码,不带字符计数),我将很高兴将此文本移至该部分;我道歉

我的最后一个问题是如何“允许独立于体重调查与死亡率的关系”。我的下一个问题只是出于好奇,如果我们要对此进行扩展,比如说如果我想独立于体重和身高来研究死亡率

我已经编写了这段代码,对于我的数据,这些残差加起来等于0,但我只是想和专家们核实一下,这是我今后参考的方法:

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

dat = np.loadtxt(sys.argv[1],dtype={"names":("SpeciesName","Mortality","Height","Weight"),"formats":("S40","f4","f4","f4")})
mymodel = smf.ols("Mortality~Height+Weight",data=dat).fit()
Residues = list(mymodel.resid_pearson)
SpeciesList = list(dat["SpeciesName"])
for species,residue in zip(SpeciesList,Residues):
    print species + "\t" + str(residue)

再次道歉,如果我写错了这一节;我不觉得这是一个新问题,作为评论,我不能添加代码;如果这更合适的话,我很乐意把这个问题变成一个新问题。

首先,我们需要找到你想要达到的目标的正确公式。然后我们可以纠正你的代码残差应该和0,你的数字似乎不这样做。另一方面,您的给定输出似乎与代码断开,因为您的代码中没有任何内容在您的模型中打印单词“Sample”
detairity
是独立变量,
Weight
是因变量。我认为如果你不认为一个人的死亡概率/可能性会影响他们的体重,那应该是另一种情况。谢谢@约翰科勒曼关于残差应该加在0上的提示非常好,谢谢;使用下面的代码,我手动添加了单词“sample”,只是为了解释我的答案是什么;我现在明白了,这可能令人困惑,我不会再这样做了;将只打印准确的输出。非常感谢大家。我认为您也应该更改for循环
pred_i=float(i)*float(slope)+float(intercept)
仍然是错误的,因为
i
迭代的是
死亡率
,而不是
重量
。是的,我在回答后也看到了它。我已经编辑过了。谢谢你,现在残差加起来是0,数字看起来并没有那么高。我一定会更多地关注numpy;我可以看出它在这里是多么有用。我可以问一下,我只是说标准化将涉及剩余平均值/SE。我知道我现在已经计算了残差,我不认为我已经将它们标准化了。我理解这意味着,对于每个剩余;减去平均值,然后除以标准误差。我能看到标准错误;我不确定我应该使用哪种方法,我假设obs_值列表的平均值?那么,对于每个残差,减去obs_值列表的平均值,然后除以标准误差?
# This calculates the regression equation.
slope, intercept, r_value, p_value, std_err = scipy.stats.linregress(x=Weight, y=Mortality)

# This calculates the predicted value for each observed value
obs_values = Mortality
pred_values = []
for i in Weight:
    pred_i = float(i) * float(slope) + float(intercept)
    pred_values.append(pred_i)
import numpy as np
from scipy import stats
import sys

# This reads in the weight and mortality data to two arrays.
arr = np.loadtxt(sys.argv[1])
Weight = arr[:,-2]
Mortality = arr[:,-1]

# This calculates the regression equation.
slope, intercept, r_value, p_value, std_err = stats.linregress(x=Weight,y=Mortality)

# This calculates the predicted value for each observed value
obs_values = Mortality
pred_values = slope * Weight + intercept

# This prints the residual for each pair of observations
Residual = obs_values - pred_values
print(Residuals)
import numpy as np
import statsmodels.formula.api as smf
import sys

dat = np.loadtxt(sys.argv[1],dtype={"names":("SpeciesName","Mortality","Height","Weight"),"formats":("S40","f4","f4","f4")})
mymodel = smf.ols("Mortality~Height+Weight",data=dat).fit()
Residues = list(mymodel.resid_pearson)
SpeciesList = list(dat["SpeciesName"])
for species,residue in zip(SpeciesList,Residues):
    print species + "\t" + str(residue)