Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - Need help in solving "Load the R data set mtcars as a pandas dataframe." problem

I am working on this problem and unsure on how to proceed.

Load the R data set mtcars as a pandas dataframe. Build a linear regression model by considering the log of independent variable wt, and log of dependent variable mpg. Fit the model with data.

Perform ANOVA on the linear model obtained in the previous step.(Hint:Use anova.anova_lm)

Display the F-statistic value.

I see in another post below solution was provided. But it doesn't to seem work.

import statsmodels.api as sm
import numpy as np
mtcars = sm.datasets.get_rdataset('mtcars')
mtcars_data = mtcars.data
liner_model = sm.formula.ols('np.log(wt) ~ np.log(mpg)',mtcars_data)
liner_result = liner_model.fit()
print(liner_result.rsquared)'''
like image 530
NARMADA SEKHAR Avatar asked Oct 30 '25 06:10

NARMADA SEKHAR


1 Answers

fixed it

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

mtcars = sm.datasets.get_rdataset("mtcars", "datasets", cache=True).data
df = pd.DataFrame(mtcars)
model = smf.ols(formula='np.log(mpg) ~ np.log(wt)', data=mtcars).fit()
print(anova.anova_lm(model))
print(anova.anova_lm(model).F["np.log(wt)"])
like image 120
NARMADA SEKHAR Avatar answered Nov 03 '25 00:11

NARMADA SEKHAR



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!