Search code examples
pythonstatsmodelssurvey

Least squares regression with sample weights on statsmodels


I am looking to implement OLS with sample weights on statsmodels. The specific application is the American Time Use Survey, in which sample weights adjust for demographic balances with respect to the population. If there is no direct implementation, then assistance in hard coding the estimator with sample weights would also be helpful.


Solution

  • @Mario, you could try WLS from statsmodels

    An example from documentation.

    import numpy as np
    import statsmodels.api as sm
    
    Y = [1,3,4,5,2,3,4]
    X = range(1,8)
    X = sm.add_constant(X)
    wls_model = sm.WLS(Y,X, weights=list(range(1,8)))
    results = wls_model.fit()
    
    print (results.params)
    

    More examples here.