Home > Back-end >  How to compute the marginal effect for a logit model in Python
How to compute the marginal effect for a logit model in Python

Time:05-11

This is my code

import pandas as pd
import numpy as np
from sklearn import preprocessing
import matplotlib.pyplot as plt 

df=pd.read_excel(r"C:\Users\Action\Downloads\Python\Practice_Data\sorted_data v2.xlsx")

from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
import seaborn as sns

from sklearn.feature_selection import RFE
from sklearn.linear_model import LogisticRegression
logreg = LogisticRegression()
import statsmodels.api as sm

#name the dependent variable
y = df['public health care services']
#name the independent variables
x = df[['NGO services', 'reason for  migration', ' Sex', 'Education Level', 'Curernt Employment status', ' Monthly Income']]

#run the model
logit_model=sm.Logit(y,x)

result=logit_model.fit()
print(result.summary2())


AME = logit_model.get_margeff(at='overall', method='dydx', atexog=None, dummy=False,  count=False)
print(AME.summary())

I am receiving the error 'Logit' object has no attribute 'get_margeff'

Does anyone know how to compute the marginal effect in python?

CodePudding user response:

Try this:

logit_model=sm.Logit(y,x).fit()
print(logit_model.summary2())

AME = logit_model.get_margeff(at='overall', method='dydx', atexog=None, dummy=False,  count=False)
print(AME.summary())

CodePudding user response:

I figured out the answer This is the code

AME = result.get_margeff(at='overall', method='dydx', atexog=None, dummy=False, count=False)
#AME = logit_model.get_margeff(at = "overall", method = "dydx")
print(AME.summary())
  • Related