gpt4 book ai didi

r - 线性回归 r 比较多个观察值与单个观察值

转载 作者:行者123 更新时间:2023-12-04 12:08:57 24 4
gpt4 key购买 nike

根据我的 question 的回答,我应该为以下 2 个模型获得相同的截距值和回归系数。但它们不一样。到底是怎么回事?

我的代码有问题吗?还是原来的答案错了?

#linear regression average qty per price point vs all quantities

x1=rnorm(30,20,1);y1=rep(3,30)
x2=rnorm(30,17,1.5);y2=rep(4,30)
x3=rnorm(30,12,2);y3=rep(4.5,30)
x4=rnorm(30,6,3);y4=rep(5.5,30)
x=c(x1,x2,x3,x4)
y=c(y1,y2,y3,y4)
plot(y,x)
cor(y,x)
fit=lm(x~y)
attributes(fit)
summary(fit)

xdum=c(20,17,12,6)
ydum=c(3,4,4.5,5.5)
plot(ydum,xdum)
cor(ydum,xdum)
fit1=lm(xdum~ydum)
attributes(fit1)
summary(fit1)


> summary(fit)

Call:
lm(formula = x ~ y)

Residuals:
Min 1Q Median 3Q Max
-8.3572 -1.6069 -0.1007 2.0222 6.4904

Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 40.0952 1.1570 34.65 <2e-16 ***
y -6.1932 0.2663 -23.25 <2e-16 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 2.63 on 118 degrees of freedom
Multiple R-squared: 0.8209, Adjusted R-squared: 0.8194
F-statistic: 540.8 on 1 and 118 DF, p-value: < 2.2e-16

> summary(fit1)

Call:
lm(formula = xdum ~ ydum)

Residuals:
1 2 3 4
-0.9615 1.8077 -0.3077 -0.5385

Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 38.2692 3.6456 10.497 0.00895 **
ydum -5.7692 0.8391 -6.875 0.02051 *
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 1.513 on 2 degrees of freedom
Multiple R-squared: 0.9594, Adjusted R-squared: 0.9391
F-statistic: 47.27 on 1 and 2 DF, p-value: 0.02051

最佳答案

您没有以可比较的方式计算 xdumydum,因为 rnorm 只会近似于您指定的平均值,尤其是当您仅抽样 30 例。然而,这很容易修复:

coef(fit)
#(Intercept) y
# 39.618472 -6.128739

xdum <- c(mean(x1),mean(x2),mean(x3),mean(x4))
ydum <- c(mean(y1),mean(y2),mean(y3),mean(y4))
coef(lm(xdum~ydum))
#(Intercept) ydum
# 39.618472 -6.128739

关于r - 线性回归 r 比较多个观察值与单个观察值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38820593/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com