Python-需要帮助解决“将R数据集mtcars作为pandas数据框加载。”问题

问题描述 投票:0回答:1

我正在研究此问题,不确定如何继续。

将R数据集mtcars加载为熊猫数据框。通过考虑自变量wt的对数和因变量mpg的对数来构建线性回归模型。用数据拟合模型。

对上一步中获得的线性模型执行ANOVA。(提示:使用anova.anova_lm)

显示F统计值。

我在下面的另一篇文章中看到提供了解决方案。但这似乎不起作用。

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)'''
python-3.x linear-regression anova
1个回答
0
投票

已修复

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)"])
© www.soinside.com 2019 - 2024. All rights reserved.