-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRegression_Models.py
More file actions
77 lines (57 loc) · 1.88 KB
/
Regression_Models.py
File metadata and controls
77 lines (57 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# -*- coding: utf-8 -*-
"""Untitled3.ipynb
Automatically generated by Colab.
Original file is located at
https://colab.research.google.com/drive/13piZCzOIOSr3p8n-TyHnCUKzwQHJWZeC
"""
# A1. 1,2 ChickWeight
import pandas as pd
import statsmodels.api as sm
import statsmodels.formula.api as smf
# upload the data
df = sm.datasets.get_rdataset("ChickWeight", "datasets").data
df.head()
#linear regression model
model1 = smf.ols('weight ~ Time', data=df).fit()
# R²
print(f"\nCoefficient of Determination (R²): {model1.rsquared:.3f}")
if model1.pvalues['Time'] < 0.05:
print("\nThere is a significant relationship between Time and Weight at the 0.05 significance level.")
else:
print("\nThere is no significant relationship between Time and Weight at the 0.05 significance level.")
print(model1.summary())
# A2
df_women = sm.datasets.get_rdataset("women", "datasets").data
df_women.head()
#linear regression model
model2 = smf.ols('weight ~ height', data=df_women).fit()
print(model2.summary())
new_heights = pd.DataFrame({'height': [60, 65, 70]})
predicted_weights = model2.predict(new_heights)
print("\nPredicted weights for heights 60, 65, 70:")
print(predicted_weights)
#A3
import pandas as pd
import statsmodels.api as sm
# read the file
fish = pd.read_csv("/content/Fish.csv")
fish.head()
#identifying independent and dependent variables
X = fish[["Length1", "Length2", "Length3", "Height", "Width"]]
y = fish["Weight"]
X = sm.add_constant(X)
#linear regression model
model3 = sm.OLS(y, X).fit()
print(model3.summary())
new_data = pd.DataFrame({
"Length1": [28.5, 28.4],
"Length2": [30.7, 31.0],
"Length3": [36.2, 36.2],
"Height": [14.2266, 14.2628],
"Width": [4.9594, 5.1042]
})
new_data = sm.add_constant(new_data, has_constant='add')
new_data = new_data[X.columns]
predicted_weights = model3.predict(new_data)
print("\nPredicted fish weights:")
print(predicted_weights)