Homework 6: Linear Regression#
Apply linear regression to predict chemical process outcomes.
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
from sklearn.preprocessing import StandardScaler
Problem 1: Simple Linear Regression#
You have data relating reaction temperature to product purity.
# Load simple regression data from URL
url = "https://raw.githubusercontent.com/jkitchin/s26-06642/main/dsmles/data/hw06_simple_regression.csv"
simple_data = pd.read_csv(url)
temperature = simple_data['temperature'].values
purity = simple_data['purity'].values
1a. Fit a linear regression model. Report the intercept and slope.
# Your code here
1b. Plot the data and the fitted line. Include proper labels and title.
# Your code here
1c. What purity would you predict at 500 K? Is this prediction reliable? Why or why not?
# Your code here
Problem 2: Multiple Linear Regression#
Predict reaction yield from multiple process variables.
# Load multiple regression data from URL
url = "https://raw.githubusercontent.com/jkitchin/s26-06642/main/dsmles/data/hw06_multiple_regression.csv"
data = pd.read_csv(url)
data.head()
| temperature | pressure | catalyst_loading | residence_time | yield | |
|---|---|---|---|---|---|
| 0 | 374.908024 | 1.282863 | 3.389142 | 12.584086 | 54.693902 |
| 1 | 490.142861 | 6.727694 | 0.878630 | 36.567732 | 67.896154 |
| 2 | 446.398788 | 3.829204 | 1.227329 | 37.031756 | 61.590318 |
| 3 | 419.731697 | 5.577136 | 4.543494 | 41.871495 | 77.475560 |
| 4 | 331.203728 | 9.168098 | 3.228931 | 46.304567 | 72.465092 |
2a. Split the data into 80% training and 20% test sets. Fit a multiple linear regression model.
# Your code here
2b. Report the R² score for both training and test sets. Is there evidence of overfitting?
# Your code here
2c. Calculate standardized coefficients. Which variable has the largest effect on yield?
# Your code here
2d. Create a residual plot. Does the model appear adequate?
# Your code here
Problem 3: Interpretation#
3a. Based on your model, if you want to increase yield, which variable should you adjust? By how much would yield increase if you doubled the catalyst loading (from 2.5% to 5%)?
# Your code here
3b. What are the limitations of using this linear model for process optimization?
Your answer here: