Python Forum
help seeking for prediction of fixed effect model
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
help seeking for prediction of fixed effect model
#1
now i get a formula by FE MODEL, but i wanna use it to predict the upper and lower bound of 'Y'
however, it seems that the i could not predict if based on the orginial data by commands like 'get_prediction'
because this command is for OLS, not for the panel data or panel model
if i solve it by Stata, i just need to use "predict" and it will ouput the prediction directly
so are there any other commands for my demand QAQ Heart !!!

This is my code:

filtered_df['Y_winsorized'] = winsorize(filtered_df['Y'], limits=[0.2, 0.2])

filtered_df = filtered_df.set_index(["material","date"])
exog = sm.add_constant(filtered_df[['X1','X2']])
result = PanelOLS(filtered_df['Y_winsorized'], exog, entity_effects=True, time_effects=True)
result = result.fit()
print(result)

pred = result.get_prediction(exog)
conf_int = pred.conf_int()  
pred_int = pred.conf_int(observed=True)  
this is the warning and error:
Error:
AttributeError: 'PanelEffectsResults' object has no attribute 'get_prediction'
Larz60+ write Jun-18-2024, 08:03 AM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
I have added tags for you this time. Please use BBCode tags on future posts.
Reply
#2
It appears that the get_prediction method in PanelOLS from statsmodels is not directly supporting prediction intervals for panel data models like it does for OLS (Ordinary Least Squares). In your case, since PanelOLS is designed for panel data with entity and time effects, the method might not directly provide prediction intervals as with simple OLS.

To predict bounds (upper and lower) for 'Y' using your model results, you may need to calculate them manually based on the model's coefficients and standard errors. Here's a general approach:

Extract Coefficients and Standard Errors: Retrieve the coefficients (result.params) and their standard errors (result.std_errors).

Calculate Prediction Interval: Use these coefficients and standard errors to compute the prediction interval manually. For instance, for a given confidence level (e.g., 95%), the upper and lower bounds can be calculated as:

makefile
Copy code
lower_bound = result.predict(exog) - 1.96 * result.std_errors
upper_bound = result.predict(exog) + 1.96 * result.std_errors
Adjust the multiplier (1.96 for 95% confidence) based on your desired confidence level.

Stata Comparison: In Stata, the equivalent of predict provides this directly, but in Python's statsmodels, you'll typically need to compute these intervals explicitly.

Here’s an example assuming result is your PanelOLS model result:

python
Copy code
# Assuming 'result' is already fitted PanelOLS model
predicted_values = result.predict(exog)
std_errors = result.std_errors

# Calculate prediction interval (e.g., 95% confidence)
confidence_level = 1.96  # For 95% confidence interval
lower_bound = predicted_values - confidence_level * std_errors
upper_bound = predicted_values + confidence_level * std_errors

# Print or use the bounds as needed
print("Lower Bound:", lower_bound)
print("Upper Bound:", upper_bound)
This approach manually computes the prediction bounds based on the model’s predictions and standard errors. Ensure that exog contains the correct variables and constants required for prediction.

If you need further assistance or clarification on implementing this, feel free to ask!

Best regards,
Gribouillis write Jun-23-2024, 08:14 AM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Bug Copying methods to effect the new owner instead of the old instance Daniel285 2 300 Jun-03-2024, 07:58 AM
Last Post: Gribouillis
  Win or Loss Prediction ksasi2k3 4 568 Apr-25-2024, 01:46 PM
Last Post: ksasi2k3
  Seeking advice on dask distributed sawtooth500 4 553 Apr-15-2024, 11:07 PM
Last Post: sawtooth500
  model.fit and model.predict errors hatflyer 6 1,766 Nov-10-2023, 01:39 AM
Last Post: hatflyer
  How to do "fixed size" (wrapping) math in Python? AlexanderWulf 13 2,364 Jul-19-2023, 04:13 PM
Last Post: deanhystad
  Fixed colum width for rowLabels i Matplotlib pandabay 0 526 Jun-10-2023, 03:40 PM
Last Post: pandabay
  Printing effect sizes for variables in an anova eyavuz21 2 1,122 Feb-01-2023, 02:12 PM
Last Post: eyavuz21
  Help with TypeWriter Effect in Python Rich Extra 0 1,307 May-23-2022, 09:44 PM
Last Post: Extra
  Encrypt and decrypt in python using own fixed key SriRajesh 3 5,531 Feb-20-2022, 01:18 PM
Last Post: dboxall123
  hyperparameters do not make a difference in prediction jenya56 0 1,068 Sep-15-2021, 12:53 PM
Last Post: jenya56

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020