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

如何从R语言lm线性回归模型fit中提取截距项系数?

Extracting Intercept Coefficient from an lm Model in R

Hey there! I see you're trying to pull the intercept coefficient from your linear regression model, and your initial syntax isn't working—let's fix that right away.

First, a quick recap of your model setup:

fit <- lm(R1 ~ R2)

Your original attempt fit["coefficients" -> "(Intercept)"] uses incorrect R syntax (-> is an assignment operator, not an indexing tool). Here are several straightforward, correct ways to extract the intercept estimate from your model:

1. Directly access the coefficients vector from the fit object

Use the dollar sign ($) to access the coefficients list, then index by either the coefficient name or its position:

# Extract by the intercept's explicit name
fit$coefficients["(Intercept)"]

# Extract by position (the intercept is always the first coefficient in this model formula)
fit$coefficients[1]

2. Use the coef() helper function (more readable)

R has a built-in function specifically for accessing model coefficients, which makes your code cleaner:

# Get the intercept with its name attached
coef(fit)["(Intercept)"]

# If you want just the raw numeric value (no name label), use double brackets:
coef(fit)[["(Intercept)"]]

3. Extract from the model summary (if you need additional stats)

If you ever want to pull the intercept alongside its standard error, p-value, or t-statistic, you can access the summary's coefficient table:

# Grab only the intercept estimate from the summary table
summary(fit)$coefficients["(Intercept)", "Estimate"]

All these methods will return the intercept value 0.003785 from your model results.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 11:39:19