-
Notifications
You must be signed in to change notification settings - Fork 0
/
GBC Birth BMI Linier Regression.py
65 lines (46 loc) · 1.95 KB
/
GBC Birth BMI Linier Regression.py
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
import os
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import statsmodels.api as sm
from google.cloud import bigquery
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = r"C:\Users\da046634\AppData\Local\Temp\MicrosoftEdgeDownloads\0edc4907-b6aa-48a7-af8c-4b7ced419855\vernal-tempo-410309-daa86de57214.json"
client = bigquery.Client()
query = """
SELECT
Year,
County_of_Residence,
County_of_Residence_FIPS,
AVG(Ave_Birth_Weight_gms) AS Avg_Birth_Weight,
AVG(Ave_Pre_pregnancy_BMI) AS Avg_Pre_Pregnancy_BMI
FROM
`bigquery-public-data.sdoh_cdc_wonder_natality.county_natality`
GROUP BY
Year, County_of_Residence, County_of_Residence_FIPS
ORDER BY
Year, County_of_Residence
"""
try:
query_job = client.query(query)
results = query_job.result()
df = results.to_dataframe()
df.dropna(subset=['Avg_Pre_Pregnancy_BMI', 'Avg_Birth_Weight'], inplace=True)
correlation = df['Avg_Pre_Pregnancy_BMI'].corr(df['Avg_Birth_Weight'])
print(f"Pearson correlation coefficient: {correlation}")
X = df['Avg_Pre_Pregnancy_BMI']
y = df['Avg_Birth_Weight']
X = sm.add_constant(X)
model = sm.OLS(y, X).fit()
print(model.summary())
plt.figure(figsize=(10, 6))
sns.scatterplot(data=df, x='Avg_Pre_Pregnancy_BMI', y='Avg_Birth_Weight')
plt.title('Scatter plot of Average Pre-Pregnancy BMI vs Average Birth Weight')
plt.xlabel('Average Pre-Pregnancy BMI')
plt.ylabel('Average Birth Weight (grams)')
sns.regplot(data=df, x='Avg_Pre_Pregnancy_BMI', y='Avg_Birth_Weight', scatter=False, color='red')
plt.axhline(y=df['Avg_Birth_Weight'].mean(), color='r', linestyle='--', label='Mean Birth Weight')
plt.axvline(x=df['Avg_Pre_Pregnancy_BMI'].mean(), color='g', linestyle='--', label='Mean Pre-Pregnancy BMI')
plt.legend()
plt.show()
except Exception as e:
print(f"An error occurred: {e}")