Truncated Gaussian Regression Models
Project description
pytruncreg Overview
The pytruncreg
package developed by Harvard University's CausaLab is designed to estimate one-way truncated gaussian regression models. The truncreg
function requires formula
, data
, a truncation point
and direction
of truncation. This python package is a translation of the original truncreg
R package CRAN by Yves Croissant and Achim Zeileis.
Parameters
- formula (str): String describing the model to be fitted in format
y ~ x_1 + x_2 + ... x_n
- data (DataFrame): The dataset containing the variables specified in the formula
- point (float): The point of truncation in
y
- direction (str): Specifies the direction of truncation, taking values
"left"
or"right"
- scaled (bool, optional): Default is
False
. When set toTrue
the model will use a scaled version of the dependent variable - iterlim (int, optional): Default is 50. The maximum iterations for the optimization algorithm.
Returns
OptimizeResult
: an object containing the results of the results of the optimization algorithmSE
an array containing the standard errors for intercept and coefficientsvcov
the hessian (variance-covariance matrix) from the optimization process
Description
The truncreg
function based on the original truncreg
R package CRAN by Yves Croissant and Achim Zeileis, performs maximum likelihood estimation of a truncated gaussian regression model. The function supports both left and right truncation and will extract variables inside of formula
from the provided data
then constructs the model matrix by supplying an intercept and fits the truncated regression model using the L-BFGS-B optimization algorithm. Internally, the function calculates the log-likelihood, gradient, and Hessian matrix for the optimization process. The initial values for beta coefficients are estimated using ordinary least squares.\
In the case of overflow, truncreg
will attempt to handle potential overflow issues in calculating the Mills ratio and log-likelihood. If scaled=True
, the dependent variable is scaled before estimation.
Example Usage
import pandas as pd
import numpy as np
from pytruncreg import truncreg
np.random.seed(42)
data = pd.DataFrame({
'y': np.random.normal(0, 1, 1000),
'x1': np.random.normal(1, 3, 1000),
'x2': np.random.normal(10, 3, 1000)
})
result = truncreg(formula='y~x1+x2', data=data, point=-4, direction='left')
print(result)
Dependencies
- numpy
- scipy.stats
- scipy.optimize
- re
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file pytruncreg-1.0.0.tar.gz
.
File metadata
- Download URL: pytruncreg-1.0.0.tar.gz
- Upload date:
- Size: 4.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.0 CPython/3.12.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 27091c218d413646060480ad9ea62aedc1cf5c0f92e748a9e5a34d8b7b9829a5 |
|
MD5 | 2657640e6326e93ef14091954898252d |
|
BLAKE2b-256 | 9b732ba6bc9ab49cb2c1156b63f09bb3c49626284b47e37620d786e7c53d538a |
File details
Details for the file pytruncreg-1.0.0-py3-none-any.whl
.
File metadata
- Download URL: pytruncreg-1.0.0-py3-none-any.whl
- Upload date:
- Size: 5.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.0 CPython/3.12.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b2952784a8e02bd797ab766c62065c631b0b08e0970a90740c510b5b70296f8c |
|
MD5 | 09edd0fce1eeaf9f10d366df3c1383e5 |
|
BLAKE2b-256 | f6657bfffaf27312db6182989b8e09c86afeaa77ea1e1231b4dd090d68915d9b |