Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Seeming Unrelated Regression weights and out of sample prediction #214

Closed
tra6sdc opened this issue Oct 3, 2019 · 5 comments
Closed

Seeming Unrelated Regression weights and out of sample prediction #214

tra6sdc opened this issue Oct 3, 2019 · 5 comments

Comments

@tra6sdc
Copy link

tra6sdc commented Oct 3, 2019

Hello,

I am interested in performing seemingly unrelated regression using linearmodels. I would like to be able to weight the observations in the model and obtain out-of-sample predictions. Is this possible? I have adapted the example code to begin to explore the possibilities but not progressed and I don't know if this is because what I want to do isn't possible.

import pandas as pd
import numpy as np
data = pd.DataFrame(np.random.randn(500, 4), columns=['y1', 'x1_1', 'y2', 'x2_1'])
weight = pd.DataFrame(np.random.randn(500, 1), columns=['weight'])
from linearmodels.system import SUR
formula = {'eq1': 'y1 ~ 1 + x1_1', 'eq2': 'y2 ~ 1 + x2_1'}
mod = SUR.from_formula(formula, data, weights=weight)
res = mod.fit(cov_type='unadjusted')
res
#data1 = pd.DataFrame(np.random.randn(500, 4), columns=['y1', 'x1_1', 'y2', 'x2_1'])
#pre = res.predict(data1)

with error message:

C:\Program Files\Anaconda3\lib\site-packages\linearmodels\system\model.py:90: UserWarning: Weights not found for equation labels:
eq1, eq2
  warnings.warn(msg, UserWarning)

Thanks.

@bashtage
Copy link
Owner

bashtage commented Oct 3, 2019

The help says:

        weights : dict-like
            Dictionary like object (e.g. a DataFrame) containing variable
            weights.  Each entry must have the same number of observations as
            data.  If an equation label is not a key weights, the weights will
            be set to unity

so that weights should be a dictionary (or possibly a DataFrame) with columns eq1 and eq2 in your case where the column has the weights for respective weights. Let me know if this works.

@bashtage
Copy link
Owner

bashtage commented Oct 3, 2019

BTW, weights must be positive, so randn is a bad choice.

@tra6sdc
Copy link
Author

tra6sdc commented Oct 3, 2019

Thanks for the help. Here is the code with adaptions that work in regards to weights.

import pandas as pd
import numpy as np
np.random.seed(123)
data = pd.DataFrame(np.random.randn(500, 4), columns=['y1', 'x1_1', 'y2', 'x2_1'])
weight = abs(pd.DataFrame(np.random.randn(500, 2), columns=['eq1','eq2']))
from linearmodels.system import SUR
formula = {'eq1': 'y1 ~ 1 + x1_1', 'eq2': 'y2 ~ 1 + x2_1'}
mod = SUR.from_formula(formula, data, weights=weight)
res = mod.fit(cov_type='unadjusted')
res

Is out-of-sample prediction possible? Or, for such a simple model should I do the calculation "manually"?

@bashtage
Copy link
Owner

bashtage commented Oct 3, 2019

@tra6sdc
Copy link
Author

tra6sdc commented Oct 3, 2019

Here is my final code

import pandas as pd
import numpy as np
np.random.seed(123)
data = pd.DataFrame(np.random.randn(500, 4), columns=['y1', 'x1_1', 'y2', 'x2_1'])
weight = abs(pd.DataFrame(np.random.randn(500, 2), columns=['eq1','eq2']))
from linearmodels.system import SUR
formula = {'eq1': 'y1 ~ 1 + x1_1', 'eq2': 'y2 ~ 1 + x2_1'}
mod = SUR.from_formula(formula, data, weights=weight)
res = mod.fit(cov_type='unadjusted')
data1 = pd.DataFrame(np.random.randn(500, 4), columns=['y1', 'x1_1', 'y2', 'x2_1'])
pre = res.predict(data=data1)

Thanks again.

@tra6sdc tra6sdc closed this as completed Oct 3, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants