0% found this document useful (0 votes)
100 views2 pages

Statsmodels Python Examples and ANOVA

This document contains code examples using the statsmodels library in Python to perform statistical analysis and linear regression on various datasets. It includes importing datasets, fitting linear regression models, predicting values, and calculating ANOVA results.

Uploaded by

Karlo
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
100 views2 pages

Statsmodels Python Examples and ANOVA

This document contains code examples using the statsmodels library in Python to perform statistical analysis and linear regression on various datasets. It includes importing datasets, fitting linear regression models, predicting values, and calculating ANOVA results.

Uploaded by

Karlo
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
  • Statistical Models Using mtcars and iris Datasets
  • Insurance and Advanced Models

import statsmodels.

api as sm
import numpy as np
mtcars = [Link].get_rdataset('mtcars')
mtcars_data = [Link]
liner_model = [Link]('[Link](wt) ~ [Link](mpg)',mtcars_data)
liner_result = liner_model.fit()
print(liner_result.rsquared)

import [Link] as sm
import [Link] as smf
import numpy as np
import pandas as pd

df = [Link].get_rdataset("iris").data
[Link]()
[Link]()
iris_subset = iris[([Link] == "versicolor") | ([Link] ==
"virginica")].copy()

print(iris_subset.[Link]())
df_subset = df[([Link] == "versicolor") | ([Link] == "virginica" )].copy()
df_subset.Species = df_subset.[Link]({"versicolor": 1, "virginica": 0})

df_subset.rename(columns={"[Link]": "Sepal_Length", "[Link]":


"Sepal_Width", "[Link]": "Petal_Length", "[Link]": "Petal_Width"},
inplace=True)

model = [Link]("Species ~ Petal_Length + Petal_Width", data=df_subset)


result = [Link]()

df_new = [Link]({"Petal_Length": [Link](20)*0.5 + 5,


"Petal_Width": [Link](20)*0.5 + 1.7})
df_new["P-Species"] = [Link](df_new)
df_new["P-Species"].head(3)

df_new["Species"] = (df_new["P-Species"] > 0.5).astype(int)


df_new.head()

import [Link] as sa
import numpy as np
import [Link] as sfa
biopsy = [Link].get_rdataset("biopsy","MASS")
biopsy_data = [Link]
biopsy_data.rename(columns={"class":"Class"},inplace=True)
biopsy_data.Class = biopsy_data.[Link]({"benign":0,"malignant":1})
biopsy_data["V1"] = [Link](biopsy_data["V1"] - biopsy_data["V1"].min(),
biopsy_data["V1"].max() - biopsy_data["V1"].min())
log_mod1 = [Link]("V1~Class",biopsy_data)
log_res1 = log_mod1.fit()
print(log_res1.summary())

import [Link] as smf

poisson_model = [Link]('num_awards ~ math + C(prog)', awards_df)


poisson_model_result = poisson_model.fit()

import [Link] as sm
import [Link] as smf
import pandas as pd
import numpy as np
df_insurance=[Link].get_rdataset("Insurance","MASS")
df_data=df_insurance.data
insurance_model=[Link]('Claims ~ [Link](Holders)', df_data).fit()
print([Link](insurance_model.resid))

#Write your code here


import [Link] as sm
import numpy as np
import pandas as pd
import [Link] as smf
from [Link] import anova

mtcars = [Link].get_rdataset("mtcars", "datasets", cache=True).data


df = [Link](mtcars)
model = [Link](formula='mpg~wt',data=mtcars).fit()
#print(anova.anova_lm(model))
print(anova.anova_lm(model).F["wt"])

#Write your code here


import [Link] as sm
import numpy as np
import pandas as pd
import [Link] as smf
from [Link] import anova

mtcars = [Link].get_rdataset("mtcars", "datasets", cache=True).data


df = [Link](mtcars)
model = [Link](formula='[Link](mpg) ~ [Link](wt)', data=mtcars).fit()
#print(anova.anova_lm(model))
print(anova.anova_lm(model).F["[Link](wt)"])

import statsmodels.api as sm
import numpy as np
mtcars = sm.datasets.get_rdataset('mtcars')
mtcars_data = mtcars.data
liner_m
import numpy as np
df_insurance=sm.datasets.get_rdataset("Insurance","MASS")
df_data=df_insurance.data
insurance_model=smf.po

You might also like