Lesson · 40 min · Free
Linear Regression with Python
Linear Regression with Python body { font-family: Arial, sans-serif; line-height: 1.6; margin: 20px; } h1, h2 { color: #2C3E50; } pre { background-color: #ECF0F1; padding: 15px; border-radius: 5px; overflow-x: auto; } co
Linear Regression with Python
Welcome to this lesson on Linear Regression with Python, a fundamental statistical technique widely used in pharmaceutical research for understanding relationships between variables. In drug discovery, development, and even post-market surveillance, linear regression can help us analyze dose-response curves, predict drug efficacy based on molecular descriptors, or assess the impact of patient demographics on treatment outcomes. At its core, linear regression models the relationship between a dependent variable (what we want to predict) and one or more independent variables (the predictors) by fitting a linear equation to observed data. The simplest form, simple linear regression, involves one independent variable, while multiple linear regression involves two or more. The goal is to find the best-fitting straight line (or hyperplane in multiple dimensions) that minimizes the sum of squared differences between the observed and predicted values. For pharmaceutical applications, this could mean: Pharmacokinetics (PK): Predicting drug concentration over time based on dosage. Pharmacodynamics (PD): Modeling the relationship between drug concentration and its effect. Biomarker Discovery: Identifying biomarkers whose levels correlate with disease progression or treatment response. Predictive Toxicology: Estimating toxicity based on chemical properties.
Implementing Simple Linear Regression
Python, with its powerful libraries like NumPy, Pandas, and Scikit-learn, makes implementing linear regression straightforward. We'll start with a simple example: modeling the relationship between the dose of a drug and its observed efficacy. Imagine we have experimental data where we've tested different doses of a new compound and measured a specific biological response. First, we need to import the necessary libraries. numpy is essential for numerical operations, pandas for data handling, and matplotlib.pyplot for visualization. From sklearn.linear_model , we'll import LinearRegression , and from sklearn.model_selection , train_test_split for splitting our data. import numpy as np import pandas as pd import matplotlib.pyplot as plt from sklearn.linear_model import LinearRegression from sklearn.model_selection import train_test_split from sklearn.metrics import mean_squared_error, r2_score # 1. Generate synthetic data for demonstration # In a real scenario, this would be your experimental data np.random.seed(42) # for reproducibility dose = np.random.rand(100, 1) * 100 # Doses from 0 to 100 mg efficacy = 2 * dose + 5 + np.random.randn(100, 1) * 20 # Efficacy = 2*dose + 5 + some noise # 2. Create a DataFrame (optional, but good practice for structured data) data = pd.DataFrame({'Dose': dose.flatten(), 'Efficacy': efficacy.flatten()}) # 3. Prepare the data for Scikit-learn X = data[['Dose']] # Independent variable (features) y = data['Efficacy'] # Dependent variable (target) # 4. Split data into training and testing sets X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) # 5. Initialize and train the Linear Regression model model = LinearRegression() model.fit(X_train, y_train) # 6. Make predictions on the test set y_pred = model.predict(X_test) # 7. Evaluate the model mse = mean_squared_error(y_test, y_pred) r2 = r2_score(y_test, y_pred) print(f"Model Intercept: {model.intercept_:.2f}") print(f"Model Coefficient (Slope): {model.coef_[0]:.2f}") print(f"Mean Squared Error (MSE): {mse:.2f}") print(f"R-squared (R2) Score: {r2:.2f}") # 8. Visualize the results plt.figure(figsize=(10, 6)) plt.scatter(X_test, y_test, color='blue', label='Actual Efficacy') plt.plot(X_test, y_pred, color='red', linewidth=2, label='Predicted Efficacy (Regression Line)') plt.title('Dose vs. Efficacy: Simple Linear Regression') plt.xlabel('Dose (mg)') plt.ylabel('Efficacy (Units)') plt.legend() plt.grid(True) plt.show() In this code, we first generate some synthetic data to simulate a dose-efficacy study. Then, we split our data into training and testing sets. This is crucial to evaluate how well our model generalizes to unseen data, preventing overfitting. We initialize the LinearRegression model and train it using the fit() method on our training data. After training, we use predict() to get predictions on the test set. Finally, we evaluate the model using Mean Squared Error (MSE) and R-squared (R2) score, and visualize the fitted line against the actual data points. The intercept represents the predicted efficacy when the dose is zero. The coefficient (slope) indicates how much efficacy is expected to change for every one-unit increase in dose. MSE measures the average squared difference between the estimated values and the actual value. R-squared indicates the proportion of the variance in the dependent variable that is predictable from the independent variable(s).
Multiple Linear Regression for Complex Relationships
Pharmaceutical processes often involve multiple factors influencing an outcome. For example, drug efficacy might not only depend on dose but also on patient age, body mass index (BMI), or co-administered drugs. In such cases, multiple linear regression is more appropriate. We simply add more independent variables to our feature set. import numpy as np import pandas as pd from sklearn.linear_model import LinearRegression from sklearn.model_selection import train_test_split from sklearn.metrics import mean_squared_error, r2_score # 1. Generate synthetic data with multiple independent variables np.random.seed(42) num_samples = 100 dose = np.random.rand(num_samples, 1) * 100 # Dose (0-100 mg) age = np.random.randint(20, 70, num_samples).reshape(-1, 1) # Age (20-69 years) bmi = np.random.rand(num_samples, 1) * 15 + 18 # BMI (18-33) # Efficacy = 1.5*dose - 0.5*age + 2*bmi + 10 + noise efficacy = (1.5 * dose + -0.5 * age + 2 * bmi + 10 + np.random.randn(num_samples, 1) * 15).flatten() # 2. Create a DataFrame data_multi = pd.DataFrame({ 'Dose': dose.flatten(), 'Age': age.flatten(), 'BMI': bmi.flatten(), 'Efficacy': efficacy }) # 3. Prepare the data for Scikit-learn X_multi = data_multi[['Dose', 'Age', 'BMI']] # Multiple independent variables y_multi = data_multi['Efficacy'] # Dependent variable # 4. Split data into training and testing sets X_train_multi, X_test_multi, y_train_multi, y_test_multi = train_test_split( X_multi, y_multi, test_size=0.2, random_state=42 ) # 5. Initialize and train the Multiple Linear Regression model model_multi = LinearRegression() model_multi.fit(X_train_multi, y_train_multi) # 6. Make predictions on the test set y_pred_multi = model_multi.predict(X_test_multi) # 7. Evaluate the model mse_multi = mean_squared_error(y_test_multi, y_pred_multi) r2_multi = r2_score(y_test_multi, y_pred_multi) print("\n--- Multiple Linear Regression Results ---") print(f"Model Intercept: {model_multi.intercept_:.2f}") print(f"Model Coefficients (Slopes): {model_multi.coef_}") print(f"Mean Squared Error (MSE): {mse_multi:.2f}") print(f"R-squared (R2) Score: {r2_multi:.2f}") # Interpretation of coefficients: print("\nCoefficient for Dose:", model_multi.coef_[0]) print("Coefficient for Age:", model_multi.coef_[1]) print("Coefficient for BMI:", model_multi.coef_[2]) Notice that the structure of the code remains largely the same. The key difference is that X_multi now contains multiple columns, representing our different independent variables. The LinearRegression model automatically handles these additional features. The coefficients associated with each variable in multiple linear regression indicate the change in the dependent variable for a one-unit change in that specific independent variable, holding all other independent variables constant. When interpreting results in pharmaceutical research, it's crucial to consider not just the statistical significance but also the biological or clinical relevance of the coefficients. A statistically significant but clinically irrelevant effect might not warrant further investigation.
Key Takeaways
Linear regression models linear relationships between a dependent variable and one or more independent variables. It's a foundational tool in pharmaceutical research for tasks like dose-response analysis and biomarker prediction. sklearn.linear_model.LinearRegression in Python provides a robust and easy-to-use implementation. Data splitting (training/testing) is essential for evaluating model generalization and preventing overfitting. Key metrics for evaluation include Mean Squared Error (MSE) for prediction accuracy and R-squared (R2) for explained variance. Interpreting coefficients (slope) and intercept is crucial for drawing meaningful conclusions from the model.
Practice Exercise
Imagine you are analyzing data from a clinical trial where you have collected information on the "Blood Pressure Reduction (mmHg)" as the dependent variable. You also have independent variables such as "Drug Dose (mg)", "Patient Age (years)", and "Baseline BP (mmHg)". Modify the multiple linear regression code example to incorporate these variables. Generate your own synthetic data for these variables, ensuring a plausible linear relationship with Blood Pressure Reduction (e.g., higher dose, younger age, and higher baseline BP might lead to greater reduction). Train the model, evaluate its performance, and interpret the coefficients for each independent variable. What would a negative coefficient for "Patient Age" imply in this context?
Watch the full lesson — free
This topic is part of Python for Pharmaceutical Research, a complete AI-narrated video course. Press play once and watch the entire lecture like a movie.
Start the course free →