x8.24 <- read.csv("C:/Users/tony8/Downloads/x8.24.txt", sep="")
反應變數:\(price\) = 銷售價格(單位:千元)
解釋變數:
lm1 表示 2 種公司類別的迴歸線,截距、斜率都不同
lm2 表示 2 種公司類別的迴歸線,截距不同、斜率相同
lm3 表示 2 種公司類別的迴歸線,截距相同、斜率不同
lm4 表示 2 種公司類別的迴歸線,截距、斜率都相同
price=x8.24$Y
tax=x8.24$X1
location=x8.24$X2
lm1=lm(Y ~ factor(X2)*X1,data = x8.24)
summary(lm1)
##
## Call:
## lm(formula = Y ~ factor(X2) * X1, data = x8.24)
##
## Residuals:
## Min 1Q Median 3Q Max
## -10.8470 -2.1639 0.0913 1.9348 9.9836
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -126.9052 14.7225 -8.620 4.33e-12 ***
## factor(X2)1 76.0215 30.1314 2.523 0.01430 *
## X1 2.7759 0.1963 14.142 < 2e-16 ***
## factor(X2)1:X1 -1.1075 0.4055 -2.731 0.00828 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 3.893 on 60 degrees of freedom
## Multiple R-squared: 0.8233, Adjusted R-squared: 0.8145
## F-statistic: 93.21 on 3 and 60 DF, p-value: < 2.2e-16
lm2=lm(Y ~ factor(X2)+X1,data = x8.24)
summary(lm2)
##
## Call:
## lm(formula = Y ~ factor(X2) + X1, data = x8.24)
##
## Residuals:
## Min 1Q Median 3Q Max
## -11.4141 -2.2927 -0.1456 1.8678 9.2341
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -107.4597 13.5509 -7.93 5.80e-11 ***
## factor(X2)1 -6.2057 1.1933 -5.20 2.45e-06 ***
## X1 2.5165 0.1806 13.93 < 2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 4.093 on 61 degrees of freedom
## Multiple R-squared: 0.8014, Adjusted R-squared: 0.7949
## F-statistic: 123.1 on 2 and 61 DF, p-value: < 2.2e-16
lm3=lm(Y ~ factor(X2):X1,data = x8.24)
summary(lm3)
##
## Call:
## lm(formula = Y ~ factor(X2):X1, data = x8.24)
##
## Residuals:
## Min 1Q Median 3Q Max
## -11.2898 -2.3282 -0.1336 1.9151 9.1627
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -108.7558 13.3984 -8.117 2.77e-11 ***
## factor(X2)0:X1 2.5341 0.1787 14.183 < 2e-16 ***
## factor(X2)1:X1 2.4491 0.1813 13.512 < 2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 4.06 on 61 degrees of freedom
## Multiple R-squared: 0.8046, Adjusted R-squared: 0.7982
## F-statistic: 125.6 on 2 and 61 DF, p-value: < 2.2e-16
lm4=lm(Y ~ X1,data = x8.24)
summary(lm4)
##
## Call:
## lm(formula = Y ~ X1, data = x8.24)
##
## Residuals:
## Min 1Q Median 3Q Max
## -16.0655 -2.6918 0.1111 2.6900 11.0356
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -118.7938 15.9380 -7.454 3.52e-10 ***
## X1 2.6474 0.2131 12.421 < 2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 4.878 on 62 degrees of freedom
## Multiple R-squared: 0.7133, Adjusted R-squared: 0.7087
## F-statistic: 154.3 on 1 and 62 DF, p-value: < 2.2e-16
anova(lm2,lm1)#2公司類別迴歸線截距與斜率都不同
## Analysis of Variance Table
##
## Model 1: Y ~ factor(X2) + X1
## Model 2: Y ~ factor(X2) * X1
## Res.Df RSS Df Sum of Sq F Pr(>F)
## 1 61 1022.1
## 2 60 909.1 1 113 7.4578 0.008281 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
anova(lm4,lm2)#2公司類別迴歸線截距不同斜率相同
## Analysis of Variance Table
##
## Model 1: Y ~ X1
## Model 2: Y ~ factor(X2) + X1
## Res.Df RSS Df Sum of Sq F Pr(>F)
## 1 62 1475.2
## 2 61 1022.1 1 453.15 27.044 2.447e-06 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
anova(lm3,lm1)#2公司類別迴歸線截距與斜率都不同
## Analysis of Variance Table
##
## Model 1: Y ~ factor(X2):X1
## Model 2: Y ~ factor(X2) * X1
## Res.Df RSS Df Sum of Sq F Pr(>F)
## 1 61 1005.5
## 2 60 909.1 1 96.449 6.3655 0.0143 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
anova(lm4,lm3)#2公司類別迴歸線截距相同斜率不同
## Analysis of Variance Table
##
## Model 1: Y ~ X1
## Model 2: Y ~ factor(X2):X1
## Res.Df RSS Df Sum of Sq F Pr(>F)
## 1 62 1475.2
## 2 61 1005.5 1 469.7 28.493 1.463e-06 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1