help@rskworld.in +91 93305 39277
RSK World
  • Home
  • Development
    • Web Development
    • Mobile Apps
    • Software
    • Games
    • Project
  • Technologies
    • Data Science
    • AI Development
    • Cloud Development
    • Blockchain
    • Cyber Security
    • Dev Tools
    • Testing Tools
  • Blog
  • About
  • Contact

Theme Settings

Color Scheme
Display Options
Font Size
100%
Back to Project
RSK World
statsmodels-statistical
/
data
RSK World
statsmodels-statistical
Statistical Modeling with Statsmodels
data
  • README.md2.8 KB
  • econometric_data.csv1.2 KB
  • hypothesis_test_data.csv368 B
  • panel_data.csv873 B
  • sample_data.csv1.3 KB
  • time_series_data.csv2.1 KB
model_persistence.pyregression_analysis.pysubmit.phpconfig.pypanel_data.csv
model_persistence.py
Raw Download
Find: Go to:
"""
Model Persistence and Serialization

Author: RSK World
Website: https://rskworld.in
Email: help@rskworld.in
Phone: +91 93305 39277
"""

import pickle
import json
import numpy as np
import pandas as pd
from datetime import datetime
import os
import warnings
warnings.filterwarnings('ignore')


class ModelPersistence:
    """
    Model Persistence and Serialization Tools
    
    Author: RSK World
    Website: https://rskworld.in
    Email: help@rskworld.in
    Phone: +91 93305 39277
    """
    
    def __init__(self, model_dir='saved_models'):
        """
        Initialize model persistence
        
        Parameters:
        -----------
        model_dir : str
            Directory to save models
        """
        self.model_dir = model_dir
        if not os.path.exists(model_dir):
            os.makedirs(model_dir)
    
    def save_model(self, model, model_name, metadata=None):
        """
        Save model to disk
        
        Parameters:
        -----------
        model : object
            Model object to save
        model_name : str
            Name for the model
        metadata : dict
            Additional metadata to save
        """
        timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
        filename = f"{model_name}_{timestamp}.pkl"
        filepath = os.path.join(self.model_dir, filename)
        
        # Prepare save data
        save_data = {
            'model': model,
            'model_name': model_name,
            'timestamp': timestamp,
            'metadata': metadata or {}
        }
        
        # Save model
        with open(filepath, 'wb') as f:
            pickle.dump(save_data, f)
        
        # Save metadata as JSON
        json_filename = filename.replace('.pkl', '_metadata.json')
        json_filepath = os.path.join(self.model_dir, json_filename)
        
        json_metadata = {
            'model_name': model_name,
            'timestamp': timestamp,
            'metadata': metadata or {}
        }
        
        with open(json_filepath, 'w') as f:
            json.dump(json_metadata, f, indent=2, default=str)
        
        print(f"Model saved to: {filepath}")
        print(f"Metadata saved to: {json_filepath}")
        
        return filepath
    
    def load_model(self, filepath):
        """
        Load model from disk
        
        Parameters:
        -----------
        filepath : str
            Path to saved model file
        """
        with open(filepath, 'rb') as f:
            save_data = pickle.load(f)
        
        print(f"Model loaded: {save_data['model_name']}")
        print(f"Saved on: {save_data['timestamp']}")
        if save_data['metadata']:
            print(f"Metadata: {save_data['metadata']}")
        
        return save_data['model'], save_data['metadata']
    
    def list_saved_models(self):
        """List all saved models"""
        models = []
        for filename in os.listdir(self.model_dir):
            if filename.endswith('.pkl'):
                filepath = os.path.join(self.model_dir, filename)
                file_size = os.path.getsize(filepath)
                mod_time = datetime.fromtimestamp(os.path.getmtime(filepath))
                
                models.append({
                    'filename': filename,
                    'filepath': filepath,
                    'size': file_size,
                    'modified': mod_time
                })
        
        if models:
            df = pd.DataFrame(models)
            print("Saved Models:")
            print("=" * 70)
            print(df.to_string(index=False))
        else:
            print("No saved models found.")
        
        return models
    
    def delete_model(self, filepath):
        """Delete saved model"""
        if os.path.exists(filepath):
            os.remove(filepath)
            # Also remove metadata JSON if exists
            json_filepath = filepath.replace('.pkl', '_metadata.json')
            if os.path.exists(json_filepath):
                os.remove(json_filepath)
            print(f"Model deleted: {filepath}")
        else:
            print(f"File not found: {filepath}")
    
    def export_model_summary(self, model, filepath):
        """
        Export model summary to text file
        
        Parameters:
        -----------
        model : object
            Model with summary() method
        filepath : str
            Output file path
        """
        import sys
        from io import StringIO
        
        # Capture summary output
        old_stdout = sys.stdout
        sys.stdout = summary_capture = StringIO()
        
        try:
            model.summary()
            summary_text = summary_capture.getvalue()
        finally:
            sys.stdout = old_stdout
        
        # Write to file
        with open(filepath, 'w') as f:
            f.write(summary_text)
        
        print(f"Model summary exported to: {filepath}")
    
    def save_predictions(self, predictions, filepath, index=None):
        """
        Save predictions to CSV
        
        Parameters:
        -----------
        predictions : array-like
            Predictions to save
        filepath : str
            Output file path
        index : array-like
            Index for predictions
        """
        df = pd.DataFrame({
            'predictions': predictions
        }, index=index)
        
        df.to_csv(filepath)
        print(f"Predictions saved to: {filepath}")


if __name__ == "__main__":
    # Example usage
    print("Model Persistence Example")
    print("=" * 70)
    
    from regression_analysis import LinearRegressionModel
    import numpy as np
    
    # Create and fit a model
    np.random.seed(42)
    X = np.random.randn(100, 3)
    y = 2 + 1.5 * X[:, 0] + 0.8 * X[:, 1] - 0.5 * X[:, 2] + np.random.randn(100) * 0.5
    
    model = LinearRegressionModel()
    model.fit(X, y)
    
    # Save model
    persistence = ModelPersistence()
    metadata = {
        'n_samples': len(X),
        'n_features': X.shape[1],
        'r_squared': model.results.rsquared
    }
    
    filepath = persistence.save_model(model, 'linear_regression', metadata)
    
    # List saved models
    persistence.list_saved_models()
    
    # Load model
    loaded_model, loaded_metadata = persistence.load_model(filepath)
    
    # Export summary
    persistence.export_model_summary(loaded_model, 'model_summary.txt')

233 lines•6.5 KB
python
regression_analysis.py
Raw Download
Find: Go to:
"""
Linear and Generalized Linear Regression Analysis using Statsmodels

Author: RSK World
Website: https://rskworld.in
Email: help@rskworld.in
Phone: +91 93305 39277
"""

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from statsmodels.formula.api import ols
from statsmodels.api import OLS, add_constant
from statsmodels.genmod.generalized_linear_model import GLM
from statsmodels.genmod import families
from statsmodels.stats.outliers_influence import variance_inflation_factor
from statsmodels.stats.diagnostic import het_breuschpagan
from statsmodels.stats.stattools import durbin_watson
import warnings
warnings.filterwarnings('ignore')


class LinearRegressionModel:
    """
    Linear Regression Model using Statsmodels OLS
    
    Author: RSK World
    Website: https://rskworld.in
    Email: help@rskworld.in
    Phone: +91 93305 39277
    """
    
    def __init__(self):
        self.model = None
        self.results = None
        self.X = None
        self.y = None
        
    def fit(self, X, y, add_intercept=True):
        """
        Fit linear regression model
        
        Parameters:
        -----------
        X : array-like
            Independent variables
        y : array-like
            Dependent variable
        add_intercept : bool
            Whether to add intercept term
        """
        self.X = X
        self.y = y
        
        if add_intercept:
            X_with_const = add_constant(X)
        else:
            X_with_const = X
            
        self.model = OLS(y, X_with_const)
        self.results = self.model.fit()
        return self.results
    
    def summary(self):
        """Print model summary"""
        if self.results is not None:
            print(self.results.summary())
        else:
            print("Model not fitted yet. Call fit() first.")
    
    def predict(self, X_new, add_intercept=True):
        """
        Make predictions on new data
        
        Parameters:
        -----------
        X_new : array-like
            New independent variables
        add_intercept : bool
            Whether to add intercept term
        """
        if self.results is None:
            raise ValueError("Model not fitted yet. Call fit() first.")
        
        if add_intercept:
            X_new = add_constant(X_new, has_constant='add')
        
        return self.results.predict(X_new)
    
    def get_residuals(self):
        """Get model residuals"""
        if self.results is None:
            raise ValueError("Model not fitted yet. Call fit() first.")
        return self.results.resid
    
    def get_fitted_values(self):
        """Get fitted values"""
        if self.results is None:
            raise ValueError("Model not fitted yet. Call fit() first.")
        return self.results.fittedvalues
    
    def plot_residuals(self):
        """Plot residual analysis"""
        if self.results is None:
            raise ValueError("Model not fitted yet. Call fit() first.")
        
        residuals = self.get_residuals()
        fitted = self.get_fitted_values()
        
        fig, axes = plt.subplots(2, 2, figsize=(12, 10))
        
        # Residuals vs Fitted
        axes[0, 0].scatter(fitted, residuals, alpha=0.6)
        axes[0, 0].axhline(y=0, color='r', linestyle='--')
        axes[0, 0].set_xlabel('Fitted Values')
        axes[0, 0].set_ylabel('Residuals')
        axes[0, 0].set_title('Residuals vs Fitted')
        axes[0, 0].grid(True, alpha=0.3)
        
        # Q-Q Plot
        from scipy import stats
        stats.probplot(residuals, dist="norm", plot=axes[0, 1])
        axes[0, 1].set_title('Q-Q Plot')
        axes[0, 1].grid(True, alpha=0.3)
        
        # Residuals Histogram
        axes[1, 0].hist(residuals, bins=30, edgecolor='black', alpha=0.7)
        axes[1, 0].set_xlabel('Residuals')
        axes[1, 0].set_ylabel('Frequency')
        axes[1, 0].set_title('Residuals Distribution')
        axes[1, 0].grid(True, alpha=0.3)
        
        # Scale-Location Plot
        sqrt_abs_residuals = np.sqrt(np.abs(residuals))
        axes[1, 1].scatter(fitted, sqrt_abs_residuals, alpha=0.6)
        axes[1, 1].set_xlabel('Fitted Values')
        axes[1, 1].set_ylabel('√|Standardized Residuals|')
        axes[1, 1].set_title('Scale-Location Plot')
        axes[1, 1].grid(True, alpha=0.3)
        
        plt.tight_layout()
        plt.show()
    
    def check_multicollinearity(self):
        """Check for multicollinearity using VIF"""
        if self.results is None:
            raise ValueError("Model not fitted yet. Call fit() first.")
        
        X_with_const = add_constant(self.X) if hasattr(self.X, 'shape') else add_constant(pd.DataFrame(self.X))
        vif_data = pd.DataFrame()
        vif_data["Variable"] = X_with_const.columns
        vif_data["VIF"] = [variance_inflation_factor(X_with_const.values, i) 
                          for i in range(X_with_const.shape[1])]
        
        print("\nVariance Inflation Factor (VIF):")
        print(vif_data)
        print("\nVIF > 10 indicates multicollinearity")
        return vif_data
    
    def check_heteroscedasticity(self):
        """Check for heteroscedasticity using Breusch-Pagan test"""
        if self.results is None:
            raise ValueError("Model not fitted yet. Call fit() first.")
        
        lm, lm_pvalue, fvalue, f_pvalue = het_breuschpagan(self.results.resid, 
                                                           self.results.model.exog)
        
        print("\nBreusch-Pagan Test for Heteroscedasticity:")
        print(f"LM Statistic: {lm:.4f}")
        print(f"LM p-value: {lm_pvalue:.4f}")
        print(f"F Statistic: {fvalue:.4f}")
        print(f"F p-value: {f_pvalue:.4f}")
        
        if f_pvalue < 0.05:
            print("Warning: Heteroscedasticity detected (p < 0.05)")
        else:
            print("No significant heteroscedasticity detected")
        
        return {'lm': lm, 'lm_pvalue': lm_pvalue, 'fvalue': fvalue, 'f_pvalue': f_pvalue}
    
    def check_autocorrelation(self):
        """Check for autocorrelation using Durbin-Watson test"""
        if self.results is None:
            raise ValueError("Model not fitted yet. Call fit() first.")
        
        dw = durbin_watson(self.results.resid)
        
        print("\nDurbin-Watson Test for Autocorrelation:")
        print(f"Durbin-Watson Statistic: {dw:.4f}")
        
        if dw < 1.5:
            print("Warning: Positive autocorrelation detected")
        elif dw > 2.5:
            print("Warning: Negative autocorrelation detected")
        else:
            print("No significant autocorrelation detected")
        
        return dw


class GLMModel:
    """
    Generalized Linear Model using Statsmodels GLM
    
    Author: RSK World
    Website: https://rskworld.in
    Email: help@rskworld.in
    Phone: +91 93305 39277
    """
    
    def __init__(self, family=families.Gaussian()):
        self.family = family
        self.model = None
        self.results = None
        self.X = None
        self.y = None
    
    def fit(self, X, y, add_intercept=True):
        """
        Fit GLM model
        
        Parameters:
        -----------
        X : array-like
            Independent variables
        y : array-like
            Dependent variable
        add_intercept : bool
            Whether to add intercept term
        """
        self.X = X
        self.y = y
        
        if add_intercept:
            X_with_const = add_constant(X)
        else:
            X_with_const = X
        
        self.model = GLM(y, X_with_const, family=self.family)
        self.results = self.model.fit()
        return self.results
    
    def summary(self):
        """Print model summary"""
        if self.results is not None:
            print(self.results.summary())
        else:
            print("Model not fitted yet. Call fit() first.")
    
    def predict(self, X_new, add_intercept=True):
        """Make predictions on new data"""
        if self.results is None:
            raise ValueError("Model not fitted yet. Call fit() first.")
        
        if add_intercept:
            X_new = add_constant(X_new, has_constant='add')
        
        return self.results.predict(X_new)


if __name__ == "__main__":
    # Example usage
    print("Linear Regression Analysis Example")
    print("=" * 50)
    
    # Generate sample data
    np.random.seed(42)
    n = 100
    X = np.random.randn(n, 3)
    y = 2 + 1.5 * X[:, 0] + 0.8 * X[:, 1] - 0.5 * X[:, 2] + np.random.randn(n) * 0.5
    
    # Create and fit model
    model = LinearRegressionModel()
    model.fit(X, y)
    
    # Print summary
    model.summary()
    
    # Diagnostic plots
    model.plot_residuals()
    
    # Check assumptions
    model.check_multicollinearity()
    model.check_heteroscedasticity()
    model.check_autocorrelation()

289 lines•9 KB
python
data/panel_data.csv
Raw Download
Find: Go to:
entity,time,X1,X2,y
0,0,1.2,2.3,5.8
0,1,1.5,2.1,6.2
0,2,1.8,2.5,6.5
0,3,1.3,2.2,6.1
0,4,1.6,2.4,6.4
0,5,1.4,2.6,6.3
0,6,1.7,2.3,6.6
0,7,1.9,2.5,6.8
0,8,1.5,2.4,6.4
0,9,1.6,2.7,6.7
1,0,2.1,3.2,7.5
1,1,2.3,3.4,7.8
1,2,2.5,3.1,7.6
1,3,2.2,3.3,7.4
1,4,2.4,3.5,7.9
1,5,2.6,3.2,7.7
1,6,2.3,3.4,7.5
1,7,2.5,3.6,8.0
1,8,2.7,3.3,7.8
1,9,2.4,3.5,7.6
2,0,0.8,1.5,4.2
2,1,0.9,1.6,4.4
2,2,1.0,1.4,4.1
2,3,0.7,1.5,3.9
2,4,0.8,1.7,4.3
2,5,1.1,1.5,4.2
2,6,0.9,1.6,4.0
2,7,1.0,1.8,4.4
2,8,1.2,1.6,4.3
2,9,0.9,1.7,4.1
3,0,3.2,4.1,9.2
3,1,3.4,4.3,9.5
3,2,3.6,4.0,9.3
3,3,3.3,4.2,9.1
3,4,3.5,4.4,9.6
3,5,3.7,4.1,9.4
3,6,3.4,4.3,9.2
3,7,3.6,4.5,9.7
3,8,3.8,4.2,9.5
3,9,3.5,4.4,9.3
4,0,1.8,2.8,6.9
4,1,2.0,2.9,7.1
4,2,2.2,2.7,6.8
4,3,1.9,2.8,6.6
4,4,2.1,3.0,7.0
4,5,2.3,2.8,6.9
4,6,2.0,2.9,6.7
4,7,2.2,3.1,7.1
4,8,2.4,2.9,7.0
4,9,2.1,3.0,6.8

53 lines•873 B
csv
🚀 Support RSK World

Subscribe to our YouTube channel for latest tutorials & updates!



Click subscribe & support our work ❤️

About RSK World

Founded by Molla Samser, with Designer & Tester Rima Khatun, RSK World is your one-stop destination for free programming resources, source code, and development tools.

Founder: Molla Samser
Designer & Tester: Rima Khatun

Development

  • Game Development
  • Web Development
  • Mobile Development
  • AI Development
  • Development Tools

Legal

  • Terms & Conditions
  • Privacy Policy
  • Disclaimer

Contact Info

Nutanhat, Mongolkote
Purba Burdwan, West Bengal
India, 713147

+91 93305 39277

hello@rskworld.in
support@rskworld.in

© 2026 RSK World. All rights reserved.

Content used for educational purposes only. View Disclaimer