æ ¹æ®æˆ‘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
ç”案 0 :(得分:4)
您没有以å¯æ¯”较的方å¼è®¡ç®—xdum
å’Œydum
ï¼Œå› ä¸º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
ç”案 1 :(得分:2)
ç†è®ºä¸Šï¼Œå¦‚果(并且仅当)å‰ä¸€æ¨¡åž‹çš„å‡å€¼ç‰äºŽåŽä¸€æ¨¡åž‹ä¸çš„点,它们应该是相åŒçš„。
在您的型å·ä¸å¹¶éžå¦‚æ¤ï¼Œå› æ¤ç»“果略有ä¸åŒã€‚例如x1
çš„å¹³å‡å€¼ï¼š
x1=rnorm(30,20,1)
mean(x1)
 Â20.08353
点版本为20。
与其他rnorm
æ ·æœ¬å˜åœ¨ç±»ä¼¼çš„å¾®å°å·®å¼‚:
> mean(x2)
[1] 17.0451
> mean(x3)
[1] 11.72307
> mean(x4)
[1] 5.913274
并ä¸æ˜¯è¯´è¿™çœŸçš„很é‡è¦ï¼Œä½†ä»…仅是FYIæ ‡å‡†å‘½å法是Yæ˜¯å› å˜é‡è€ŒX是自å˜é‡ï¼Œä½ å¯ä»¥é€†è½¬ã€‚å½“ç„¶æ²¡æœ‰åŒºåˆ«ï¼Œä½†ä½ çŸ¥é“。