user2187653
user2187653

Reputation: 21

Find the F-statistic value for ANOVA first linear model

I am going through stats with python topics. I am struck with one hands on.

Problem statement:

Perform ANOVA on the first linear model obtained while working with mtcars data set. Display the F-statistic value.

What i did for the problem statement:

import statsmodels.api as sm    
import statsmodels.formula.api as smf    
from statsmodels.stats import anova

mtcars_data = sm.datasets.get_rdataset("mtcars").data    
print(mtcars_data.columns)

mt_model1 = smf.ols('mpg ~ cyl', mtcars_data ).fit()    
print(anova.anova_lm(mt_model1))

output

How can I display the F-statistic for the above problem?

Upvotes: 2

Views: 5545

Answers (5)

Rushabh Shah
Rushabh Shah

Reputation: 75

Addition to above if you are looking for log value based model fit, You need to fit model with Log and below code works.

import statsmodels.api as sm
import statsmodels.formula.api as smf
from statsmodels.stats import anova
import numpy as np

mtcars_data = sm.datasets.get_rdataset("mtcars").data
#mt_model1 = smf.ols('mpg ~ cyl', mtcars_data ).fit()

#print(mt_model1.fvalue)

lm = smf.ols('np.log(mpg) ~ np.log(wt)', mtcars_data).fit()
av = sm.stats.anova_lm(lm)
print(av.F['np.log(wt)'])

Upvotes: 0

Rushabh Shah
Rushabh Shah

Reputation: 75

Please find below code which worked for me and I passed handon. I think they asked for F-statics value of wt variable only and last print statement gives that in output file.

#Write your code here
import statsmodels.api as sm
import statsmodels.formula.api as smf
from statsmodels.stats import anova

mtcars_data = sm.datasets.get_rdataset("mtcars").data
#mt_model1 = smf.ols('mpg ~ cyl', mtcars_data ).fit()

#print(mt_model1.fvalue)

lm = smf.ols('mpg ~ wt', mtcars_data).fit()
av = sm.stats.anova_lm(lm)
print(av.F.wt)

Upvotes: 0

Data_explorer
Data_explorer

Reputation: 11

After many trials got this.

    import statsmodels.api as sm
    from statsmodels.formula.api import ols

    mtcars = sm.datasets.get_rdataset('mtcars').data

    lm = ols('mpg ~ wt', mtcars).fit()
    av = sm.stats.anova_lm(lm,type=2)
    print(av.F.wt)

Upvotes: 1

Surani Matharaarachchi
Surani Matharaarachchi

Reputation: 589

Since you have already fit the model with the desired variables in mt_model1, you can directly call for F-statistic by,

print(mt_model1.fvalue)

This can be also used when you have multiple predictors in your model.

Upvotes: 1

Hasif Seyd
Hasif Seyd

Reputation: 1694

So if you want to get the F Statistic value from the anova table for cyl attribute , so something like this

print(anova.anova_lm(mt_model1).F["cyl"])

Upvotes: 2

Related Questions