You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

请问statsmodels中是否存在R语言predict(model, newdata, type="response")的等价函数?

在statsmodels中实现R的predict(model, newdata, type="response")等价功能

R的predict(model, newdata, type="response")核心是返回响应变量原始尺度上的预测值(比如逻辑回归返回概率,而非线性预测值),statsmodels中可通过以下方式实现:

  • 对于多数回归模型(如逻辑回归、线性回归):
    默认情况下,model.predict(newdata)就等价于R的该功能——因为statsmodels的predict方法默认会将线性预测值转换为响应变量的尺度(对应参数transform=True,可显式指定)。
    示例(逻辑回归):

    import statsmodels.api as sm
    import pandas as pd
    
    # 训练模型
    data = sm.datasets.get_rdataset("mtcars").data
    y = data["am"]
    X = sm.add_constant(data[["hp", "wt"]])
    model = sm.Logit(y, X).fit()
    
    # 构造新数据
    new_data = pd.DataFrame({"hp": [150, 200], "wt": [3.0, 2.5]})
    new_data = sm.add_constant(new_data)
    
    # 获取响应尺度的预测值
    predictions = model.predict(new_data)
    
  • 对于广义线性模型(GLM):
    可直接使用model.predict(newdata, type="response"),参数名和R完全一致,功能等价。

  • 线性回归特殊情况:
    由于线性回归的响应尺度就是线性预测值,直接调用model.predict(newdata)即可得到对应结果。

内容的提问来源于stack exchange,提问作者szaki

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.11 21:51:16