使用 statsmodels pythton 的 OLS 中的语法无效

发布于 2025-01-19 09:43:59 字数 692 浏览 1 评论 0原文

我想尝试对某些数据进行回归。

country_scores=country_scores.rename(columns={"Median Math Score (TIMSS Scale, 4th Grade)": "Median Math Score"})
country_scores_log2 = country_scores.copy()
country_scores_log2['GDP Per Capita'] = np.log2(country_scores_log2['GDP Per Capita'].astype(float))
mod = smf.ols(formula="GDP Per Capita ~ Median Math Score", data=country_scores_log2)
res = mod.fit()
print(res.summary())

当我尝试此操作时,我总是会出现一个错误:

  File "C:\ProgramData\Anaconda3\lib\ast.py", line 50, in parse
    return compile(source, filename, mode, flags,

  File "<unknown>", line 1
    Median Math Score
           ^
SyntaxError: invalid syntax

I want to try regression on some data.

country_scores=country_scores.rename(columns={"Median Math Score (TIMSS Scale, 4th Grade)": "Median Math Score"})
country_scores_log2 = country_scores.copy()
country_scores_log2['GDP Per Capita'] = np.log2(country_scores_log2['GDP Per Capita'].astype(float))
mod = smf.ols(formula="GDP Per Capita ~ Median Math Score", data=country_scores_log2)
res = mod.fit()
print(res.summary())

When I try this, I always get an error saying:

  File "C:\ProgramData\Anaconda3\lib\ast.py", line 50, in parse
    return compile(source, filename, mode, flags,

  File "<unknown>", line 1
    Median Math Score
           ^
SyntaxError: invalid syntax

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

盛夏尉蓝 2025-01-26 09:43:59

通常,在我使用的包装中,模型拟合的模型拟合语法将采用不同的模型语法。尝试以下操作:

mod = smf.ols("GDP Per Capita" ~ "Median Math Score", data = country_scores_log2)

通常,我使用的语法是格式:

mod = smf.ols(y, X)

y是您的目标变量,x是一个或多个输入变量的矩阵/数据表。

我个人写的方式将是:

mod = smf.ols(country_scores_log2['GDP Per Capita'], country_scores_log2["Median Math Score"])

如果您想使用一个或多个其他变量弄乱不同的输入,则可以将“中值数学得分”替换为列表。

Typically, the syntax for model fits in python across the packages I have used will take a different model syntax. Try this:

mod = smf.ols("GDP Per Capita" ~ "Median Math Score", data = country_scores_log2)

In general, the syntax that I use is of the format:

mod = smf.ols(y, X)

y being your target variable and X being a matrix/data table of one or more input variables.

The way I would personally write it would be:

mod = smf.ols(country_scores_log2['GDP Per Capita'], country_scores_log2["Median Math Score"])

Where you could replace "Median Math Score" with a list if you wanted to mess around with different inputs using one or more of your other variables.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文