0

I am using statsmodels library for WLS. I am trying to get residuals but it returns value list of nan. Do anyone know what is the problem. Values in dataframe in column x and y are float.

import pandas as pd
import statsmodels.formula.api as smf

res = smf.wls('y ~ x', data=df).fit()
#get residuals
print(OLSInfluence(res).resid_studentized_external)

I found out that it works on ols, but I need it for wls. So below code returns residual values for ols

res = smf.ols('y ~ x', data=df, weights = df["weight"]).fit()
#get residuals
print(OLSInfluence(res).resid_studentized_external)
Klemz
  • 123
  • 2
  • 13
  • 1
    see https://stackoverflow.com/questions/40621686/does-statsmodels-wls-have-get-influence-function There is still no influence function in statsmodels specific for WLS. – Josef Apr 14 '20 at 16:56
  • Thanks, I managed to save my problem with that. I have one more question about WLS. I am trying to get standard error of the model with the root of res.mse_resid. Again I get the right value for the OLS and wrong value for the WLS. I calculate it manualy with formula in video https://www.youtube.com/watch?v=r-txC-dpI-E (1:15 min), which give me the right value. – Klemz Apr 15 '20 at 12:29
  • `mse_resid` and similar like `ssr` in WLS are defined based on weighted residuals `wresid`, and not in terms of residuals for the original unweighted values.. – Josef Apr 15 '20 at 13:56

1 Answers1

0

I tried the code below, which gave me the right result

import pandas as pd
import statsmodels.api as smsm.ols

wls_model = sm.WLS(list(df['y']), sm.add_constant(list(df['x'])), weights=df['weight'])
res = sm.OLS(wls_model.wendog, wls_model.wexog).fit()
#get residuals
print(OLSInfluence(res).resid_studentized_external)
Klemz
  • 123
  • 2
  • 13