code 13.1

## code 12.1의 자료를 사용함

## code 12.1
visit<-c(350,460,350,430,350,380,430,470,450,490,340,300,440,450,300)
fee<-c(5.5,7.5,8,8,6.8,7.5,4.5,6.4,7,5,7.2,7.9,5.9,5,7)
ad<-c(3.3,3.3,3,4.5,3,4,3,3.7,3.5,4,3.5,3.2,4,3.5,2.7)
region<-c("A","A","A","A","A","B","B","B","B","B","C","C","C","C","C")
interaction<-fee*ad
da<-c(1,1,1,1,1,0,0,0,0,0,0,0,0,0,0)
db<-c(0,0,0,0,0,1,1,1,1,1,0,0,0,0,0)

sale<-data.frame(visit, fee, ad, interaction, region, da, db)

result.13.1<-lm(visit ~ da + db + fee, data=sale)
summary(result.13.1)
## 
## Call:
## lm(formula = visit ~ da + db + fee, data = sale)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
## -69.98 -42.70 -14.44  42.67  78.55 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)   493.14      94.60   5.213 0.000289 ***
## da             32.79      36.14   0.907 0.383735    
## db             67.98      36.03   1.887 0.085813 .  
## fee           -19.26      13.83  -1.393 0.191021    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 55.82 on 11 degrees of freedom
## Multiple R-squared:  0.3934, Adjusted R-squared:  0.2279 
## F-statistic: 2.378 on 3 and 11 DF,  p-value: 0.1256
result.13.2<-lm(visit ~ region + fee, data=sale)
summary(result.13.2)
## 
## Call:
## lm(formula = visit ~ region + fee, data = sale)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
## -69.98 -42.70 -14.44  42.67  78.55 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)   525.93     102.09   5.152 0.000317 ***
## regionB        35.20      38.33   0.918 0.378198    
## regionC       -32.79      36.14  -0.907 0.383735    
## fee           -19.26      13.83  -1.393 0.191021    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 55.82 on 11 degrees of freedom
## Multiple R-squared:  0.3934, Adjusted R-squared:  0.2279 
## F-statistic: 2.378 on 3 and 11 DF,  p-value: 0.1256

code 13.2

library(ggplot2)

sale$predict<-predict(result.13.1)

ggplot(sale, aes(x=fee, y=predict, linetype=region)) + geom_line() 

dot1<-data.frame(sale$fee, sale$predict, sale$region)
names(dot1)<-c("fee", "predict", "region")

ggplot(sale, aes(x=fee, y=predict, linetype=region)) + geom_line() +
    geom_point(data=dot1, shape=region)

code 13.3

x<-c(97,95,92,90,89,87,86,85,85,84,83,82,81,80,79,78,76,74,71,67,84)
y<-c(93,91,87,87,84,82,83,82,88,80,84,86,78,78,75,77,75,73,81,60,83.9903)
trt<-c(1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0)

rd<-data.frame(x, y, trt)

rd  #저장된 데이터 출력
##     x       y trt
## 1  97 93.0000   1
## 2  95 91.0000   1
## 3  92 87.0000   1
## 4  90 87.0000   1
## 5  89 84.0000   1
## 6  87 82.0000   1
## 7  86 83.0000   1
## 8  85 82.0000   1
## 9  85 88.0000   1
## 10 84 80.0000   1
## 11 83 84.0000   0
## 12 82 86.0000   0
## 13 81 78.0000   0
## 14 80 78.0000   0
## 15 79 75.0000   0
## 16 78 77.0000   0
## 17 76 75.0000   0
## 18 74 73.0000   0
## 19 71 81.0000   0
## 20 67 60.0000   0
## 21 84 83.9903   0

code 13.4

rd$x2<-x-84

result.13.3<-lm(y ~ x + trt + x2*trt, data=rd)
summary(result.13.3)
## 
## Call:
## lm(formula = y ~ x + trt + x2 * trt, data = rd)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -6.0287 -1.7000 -0.4778  0.4944 10.7450 
## 
## Coefficients: (1 not defined because of singularities)
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  -4.7611    17.4455  -0.273 0.788208    
## x             1.0566     0.2240   4.717 0.000199 ***
## trt          -2.3181     2.5708  -0.902 0.379823    
## x2                NA         NA      NA       NA    
## trt:x2       -0.2510     0.3581  -0.701 0.492841    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 3.749 on 17 degrees of freedom
## Multiple R-squared:  0.7686, Adjusted R-squared:  0.7278 
## F-statistic: 18.82 on 3 and 17 DF,  p-value: 1.208e-05
rd$predict<-predict(result.13.3)
rd$trt.factor<-as.factor(rd$trt)

ggplot(rd, aes(x=x, y=y, color=trt.factor, linetype=trt.factor)) + 
    geom_point() + stat_smooth(method=lm, se=FALSE) +   
    geom_vline(xintercept=84, color="red") +
    annotate("text", x=87, y=90, label="구분점=84")

code 13.5

install.packages(“rgl”)

result.13.4<-lm(visit ~ fee + ad, data=sale)

result.13.4$coef
## (Intercept)         fee          ad 
##   306.52619   -24.97509    74.13096
result.13.4$coef[1]
## (Intercept) 
##    306.5262
result.13.4$coef[2]
##       fee 
## -24.97509
result.13.4$coef[3]
##       ad 
## 74.13096
height<-matrix(,15,15)
x.fee<-sort(sale$fee)   #좌표를 정렬함. 예쁘게 그리기 위해선 이 편이 좋음
x.ad<-sort(sale$ad)

x.fee   
##  [1] 4.5 5.0 5.0 5.5 5.9 6.4 6.8 7.0 7.0 7.2 7.5 7.5 7.9 8.0 8.0
x.ad
##  [1] 2.7 3.0 3.0 3.0 3.2 3.3 3.3 3.5 3.5 3.5 3.7 4.0 4.0 4.0 4.5
for (i in 1:nrow(sale))     #x.fee - x.ad 좌표에 따른 높이를 계산함
{
    for (j in 1:nrow(sale))
    {
        height[i,j]<-(result.13.4$coef[1] + 
                    result.13.4$coef[2]*x.fee[i] +
                    result.13.4$coef[3]*x.ad[j])/100
    }
}


library(rgl)        #없다면 install.packages("")문으로 설치함
surface3d(x=x.fee, y=x.ad, z=height, 
        alpha=0.7, back="lines", color="blue")

rgl.bbox(color="grey60", emission="grey40", xlen=0,ylen=0,zlen=0)
axes3d(edges=c("x--", "y+-", "z--"))

mtext3d("fee(thousand)",edge="x--",line=2, color="black")
mtext3d("ad(thousand)",edge="y+-",line=3, color="black")
mtext3d("visit(hundred)",edge="z--",line=3, color="black")

code 13.6, code 13.7

result.13.6<-lm(visit ~ fee + ad + fee*ad)
summary(result.13.6)
## 
## Call:
## lm(formula = visit ~ fee + ad + fee * ad)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -61.573 -32.774  -1.152  20.189  95.205 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)
## (Intercept)   88.838    585.071   0.152    0.882
## fee            6.203     82.823   0.075    0.942
## ad           136.779    167.071   0.819    0.430
## fee:ad        -8.967     23.600  -0.380    0.711
## 
## Residual standard error: 49.25 on 11 degrees of freedom
## Multiple R-squared:  0.5277, Adjusted R-squared:  0.3989 
## F-statistic: 4.096 on 3 and 11 DF,  p-value: 0.03528
result.13.6$coef
## (Intercept)         fee          ad      fee:ad 
##   88.838451    6.202687  136.779279   -8.967063
result.13.6$coef[1]
## (Intercept) 
##    88.83845
result.13.6$coef[2]
##      fee 
## 6.202687
result.13.6$coef[3]
##       ad 
## 136.7793
result.13.6$coef[4]
##    fee:ad 
## -8.967063
height<-matrix(,15,15)
x.fee<-sort(sale$fee)
x.ad<-sort(sale$ad)

x.fee
##  [1] 4.5 5.0 5.0 5.5 5.9 6.4 6.8 7.0 7.0 7.2 7.5 7.5 7.9 8.0 8.0
x.ad
##  [1] 2.7 3.0 3.0 3.0 3.2 3.3 3.3 3.5 3.5 3.5 3.7 4.0 4.0 4.0 4.5
## 원래 그림
for (i in 1:nrow(sale))
{
    for (j in 1:nrow(sale))
    {
        height[i,j]<-(result.13.6$coef[1] + 
                    result.13.6$coef[2]*x.fee[i] +
                    result.13.6$coef[3]*x.ad[j] +
                    result.13.6$coef[4]*x.fee[i]*x.ad[j])/100
    }
}


surface3d(x=x.fee, y=x.ad, z=height, 
        alpha=0.7, back="lines", color="blue")

rgl.bbox(color="grey60", emission="grey40", xlen=0,ylen=0,zlen=0)
axes3d(edges=c("x--", "y+-", "z--"))

mtext3d("fee(thousand)",edge="x--",line=2, color="black")
mtext3d("ad(thousand)",edge="y+-",line=3, color="black")
mtext3d("visit(hundred)",edge="z--",line=3, color="black")



## 확 뒤틀고 싶을 때
for (i in 1:nrow(sale))
{
    for (j in 1:nrow(sale))
    {
        height[i,j]<-(result.13.6$coef[1] + 
                    result.13.6$coef[2]*x.fee[i] +
                    result.13.6$coef[3]*x.ad[j] +
                    result.13.6$coef[4]*x.fee[i]*x.ad[j]*i*j)/15000
    }
}

code 13.8

x<-seq(1, 4, by=0.1)
y<-exp(x)
ln.x<-log(x)
ln.y<-log(y)

sim1<-data.frame(x, y)
sim2<-data.frame(x, ln.y)

head(sim1)
##     x        y
## 1 1.0 2.718282
## 2 1.1 3.004166
## 3 1.2 3.320117
## 4 1.3 3.669297
## 5 1.4 4.055200
## 6 1.5 4.481689
ggplot(sim1, aes(x=x, y=y)) + geom_line() +
    geom_line(data=sim2, aes(x=x, y=ln.y), linetype=2) +
    annotate("text", x=3.5, y=40, label="y=x")  +
    annotate("text", x=3.5, y=5, label="ln(y)=x")

code 13.9

#code 12.1의 자료 sale을 사용

#만약, 연속해서 앞의 자료를 사용하기 위해서는 다음의 subset문을 사용하여 원상태로 자료를 돌림
sale<-subset(sale, select=c("visit", "fee", "ad", "interaction", "region", "da", "db"))

sale$visit.center <- with(sale, visit - mean(visit))
sale$fee.center <- with(sale, fee - mean(fee))
sale$ad.center <- with(sale, ad - mean(ad))
sale$interaction.center <- with(sale, interaction - mean(interaction))

result.13.9.1<-lm(visit ~ ad.center + fee.center, data=sale)
summary(result.13.9.1)
## 
## Call:
## lm(formula = visit ~ ad.center + fee.center, data = sale)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -63.795 -33.796  -9.088  17.175  96.155 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)   399.33      12.26  32.585 4.42e-13 ***
## ad.center      74.13      25.97   2.855   0.0145 *  
## fee.center    -24.98      10.83  -2.306   0.0398 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 47.46 on 12 degrees of freedom
## Multiple R-squared:  0.5215, Adjusted R-squared:  0.4417 
## F-statistic: 6.539 on 2 and 12 DF,  p-value: 0.01201
result.13.9.2<-lm(visit ~ ad + fee, data=sale)
summary(result.13.9.2)
## 
## Call:
## lm(formula = visit ~ ad + fee, data = sale)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -63.795 -33.796  -9.088  17.175  96.155 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)  
## (Intercept)   306.53     114.25   2.683   0.0199 *
## ad             74.13      25.97   2.855   0.0145 *
## fee           -24.98      10.83  -2.306   0.0398 *
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 47.46 on 12 degrees of freedom
## Multiple R-squared:  0.5215, Adjusted R-squared:  0.4417 
## F-statistic: 6.539 on 2 and 12 DF,  p-value: 0.01201
sale
##    visit fee  ad interaction region da db visit.center fee.center
## 1    350 5.5 3.3       18.15      A  1  0    -49.33333 -1.1133333
## 2    460 7.5 3.3       24.75      A  1  0     60.66667  0.8866667
## 3    350 8.0 3.0       24.00      A  1  0    -49.33333  1.3866667
## 4    430 8.0 4.5       36.00      A  1  0     30.66667  1.3866667
## 5    350 6.8 3.0       20.40      A  1  0    -49.33333  0.1866667
## 6    380 7.5 4.0       30.00      B  0  1    -19.33333  0.8866667
## 7    430 4.5 3.0       13.50      B  0  1     30.66667 -2.1133333
## 8    470 6.4 3.7       23.68      B  0  1     70.66667 -0.2133333
## 9    450 7.0 3.5       24.50      B  0  1     50.66667  0.3866667
## 10   490 5.0 4.0       20.00      B  0  1     90.66667 -1.6133333
## 11   340 7.2 3.5       25.20      C  0  0    -59.33333  0.5866667
## 12   300 7.9 3.2       25.28      C  0  0    -99.33333  1.2866667
## 13   440 5.9 4.0       23.60      C  0  0     40.66667 -0.7133333
## 14   450 5.0 3.5       17.50      C  0  0     50.66667 -1.6133333
## 15   300 7.0 2.7       18.90      C  0  0    -99.33333  0.3866667
##    ad.center interaction.center
## 1      -0.18         -4.8806667
## 2      -0.18          1.7193333
## 3      -0.48          0.9693333
## 4       1.02         12.9693333
## 5      -0.48         -2.6306667
## 6       0.52          6.9693333
## 7      -0.48         -9.5306667
## 8       0.22          0.6493333
## 9       0.02          1.4693333
## 10      0.52         -3.0306667
## 11      0.02          2.1693333
## 12     -0.28          2.2493333
## 13      0.52          0.5693333
## 14      0.02         -5.5306667
## 15     -0.78         -4.1306667
cor(sale[,1:4])
##                    visit         fee         ad interaction
## visit        1.000000000 -0.44327318 0.55631986 0.009207291
## fee         -0.443273183  1.00000000 0.03043758 0.770308920
## ad           0.556319857  0.03043758 1.00000000 0.652477416
## interaction  0.009207291  0.77030892 0.65247742 1.000000000
cor(sale[,8:11])
##                    visit.center  fee.center  ad.center interaction.center
## visit.center        1.000000000 -0.44327318 0.55631986        0.009207291
## fee.center         -0.443273183  1.00000000 0.03043758        0.770308920
## ad.center           0.556319857  0.03043758 1.00000000        0.652477416
## interaction.center  0.009207291  0.77030892 0.65247742        1.000000000

code.13.10

# code 13.9의 자료를 사용

head(sale)
##   visit fee  ad interaction region da db visit.center fee.center ad.center
## 1   350 5.5 3.3       18.15      A  1  0    -49.33333 -1.1133333     -0.18
## 2   460 7.5 3.3       24.75      A  1  0     60.66667  0.8866667     -0.18
## 3   350 8.0 3.0       24.00      A  1  0    -49.33333  1.3866667     -0.48
## 4   430 8.0 4.5       36.00      A  1  0     30.66667  1.3866667      1.02
## 5   350 6.8 3.0       20.40      A  1  0    -49.33333  0.1866667     -0.48
## 6   380 7.5 4.0       30.00      B  0  1    -19.33333  0.8866667      0.52
##   interaction.center
## 1         -4.8806667
## 2          1.7193333
## 3          0.9693333
## 4         12.9693333
## 5         -2.6306667
## 6          6.9693333
sale$std.visit<-with(sale, visit.center/sd(visit))
sale$std.fee<-with(sale, fee.center/sd(fee))
sale$std.ad<-with(sale, ad.center/sd(ad))
sale$std.interaction<-with(sale, std.fee*std.ad)

result.13.10.1<-lm(std.visit ~ std.ad + std.fee, data=sale)
summary(result.13.10.1)
## 
## Call:
## lm(formula = std.visit ~ std.ad + std.fee, data = sale)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -1.0043 -0.5320 -0.1431  0.2704  1.5137 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)  
## (Intercept)  3.905e-16  1.929e-01   0.000   1.0000  
## std.ad       5.703e-01  1.998e-01   2.855   0.0145 *
## std.fee     -4.606e-01  1.998e-01  -2.306   0.0398 *
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.7472 on 12 degrees of freedom
## Multiple R-squared:  0.5215, Adjusted R-squared:  0.4417 
## F-statistic: 6.539 on 2 and 12 DF,  p-value: 0.01201
result.13.10.2<-lm(visit ~ ad + fee, data=sale)
summary(result.13.10.2)
## 
## Call:
## lm(formula = visit ~ ad + fee, data = sale)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -63.795 -33.796  -9.088  17.175  96.155 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)  
## (Intercept)   306.53     114.25   2.683   0.0199 *
## ad             74.13      25.97   2.855   0.0145 *
## fee           -24.98      10.83  -2.306   0.0398 *
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 47.46 on 12 degrees of freedom
## Multiple R-squared:  0.5215, Adjusted R-squared:  0.4417 
## F-statistic: 6.539 on 2 and 12 DF,  p-value: 0.01201

code 13.11

result.13.11.1<-lm(std.visit ~ std.ad + std.fee + std.interaction, data=sale)
summary(result.13.11.1)
## 
## Call:
## lm(formula = std.visit ~ std.ad + std.fee + std.interaction, 
##     data = sale)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.96929 -0.51593 -0.01814  0.31782  1.49873 
## 
## Coefficients:
##                  Estimate Std. Error t value Pr(>|t|)  
## (Intercept)      0.002296   0.200281   0.011   0.9911  
## std.ad           0.596085   0.218104   2.733   0.0195 *
## std.fee         -0.461142   0.207316  -2.224   0.0480 *
## std.interaction -0.080829   0.212734  -0.380   0.7112  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.7753 on 11 degrees of freedom
## Multiple R-squared:  0.5277, Adjusted R-squared:  0.3989 
## F-statistic: 4.096 on 3 and 11 DF,  p-value: 0.03528
result.13.11.2<-lm(visit ~ ad + fee + interaction, data=sale)
summary(result.13.11.2)
## 
## Call:
## lm(formula = visit ~ ad + fee + interaction, data = sale)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -61.573 -32.774  -1.152  20.189  95.205 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)
## (Intercept)   88.838    585.071   0.152    0.882
## ad           136.779    167.071   0.819    0.430
## fee            6.203     82.823   0.075    0.942
## interaction   -8.967     23.600  -0.380    0.711
## 
## Residual standard error: 49.25 on 11 degrees of freedom
## Multiple R-squared:  0.5277, Adjusted R-squared:  0.3989 
## F-statistic: 4.096 on 3 and 11 DF,  p-value: 0.03528

code 13.12

## 1, 2, 3종 제곱합

result.13.12.1<-lm(visit ~ ad + fee, data=sale)
result.13.12.2<-lm(visit ~ fee + ad, data=sale)
result.13.12.3<-lm(visit ~ ad + fee + interaction, data=sale)
result.13.12.4<-lm(visit ~ fee + ad + interaction, data=sale)

#1종 제곱합

anova(result.13.12.1)
## Analysis of Variance Table
## 
## Response: visit
##           Df Sum Sq Mean Sq F value  Pr(>F)  
## ad         1  17484 17484.2  7.7612 0.01647 *
## fee        1  11976 11975.8  5.3160 0.03979 *
## Residuals 12  27033  2252.8                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
anova(result.13.12.2)
## Analysis of Variance Table
## 
## Response: visit
##           Df Sum Sq Mean Sq F value  Pr(>F)  
## fee        1  11100 11100.4  4.9274 0.04646 *
## ad         1  18360 18359.6  8.1498 0.01449 *
## Residuals 12  27033  2252.8                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#2종 제곱합

library(car)

Anova(result.13.12.1, type=2)
## Anova Table (Type II tests)
## 
## Response: visit
##           Sum Sq Df F value  Pr(>F)  
## ad         18360  1  8.1498 0.01449 *
## fee        11976  1  5.3160 0.03979 *
## Residuals  27033 12                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Anova(result.13.12.2, type=2)
## Anova Table (Type II tests)
## 
## Response: visit
##           Sum Sq Df F value  Pr(>F)  
## fee        11976  1  5.3160 0.03979 *
## ad         18360  1  8.1498 0.01449 *
## Residuals  27033 12                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#3종 제곱합. 

Anova(result.13.12.1, type=3)
## Anova Table (Type III tests)
## 
## Response: visit
##             Sum Sq Df F value  Pr(>F)  
## (Intercept)  16215  1  7.1977 0.01993 *
## ad           18360  1  8.1498 0.01449 *
## fee          11976  1  5.3160 0.03979 *
## Residuals    27033 12                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Anova(result.13.12.2, type=3)
## Anova Table (Type III tests)
## 
## Response: visit
##             Sum Sq Df F value  Pr(>F)  
## (Intercept)  16215  1  7.1977 0.01993 *
## fee          11976  1  5.3160 0.03979 *
## ad           18360  1  8.1498 0.01449 *
## Residuals    27033 12                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#2종 제곱합. 교호작용이 있는 경우

Anova(result.13.12.3, type=2)   
## Anova Table (Type II tests)
## 
## Response: visit
##              Sum Sq Df F value Pr(>F)
## ad           1625.8  1  0.6702 0.4303
## fee            13.6  1  0.0056 0.9416
## interaction   350.2  1  0.1444 0.7112
## Residuals   26683.1 11
Anova(result.13.12.4, type=2)
## Anova Table (Type II tests)
## 
## Response: visit
##              Sum Sq Df F value Pr(>F)
## fee            13.6  1  0.0056 0.9416
## ad           1625.8  1  0.6702 0.4303
## interaction   350.2  1  0.1444 0.7112
## Residuals   26683.1 11
#2종 제곱합. 교호작용이 있는 경우

Anova(result.13.12.3, type=3)   
## Anova Table (Type III tests)
## 
## Response: visit
##              Sum Sq Df F value Pr(>F)
## (Intercept)    55.9  1  0.0231 0.8821
## ad           1625.8  1  0.6702 0.4303
## fee            13.6  1  0.0056 0.9416
## interaction   350.2  1  0.1444 0.7112
## Residuals   26683.1 11
Anova(result.13.12.4, type=3)
## Anova Table (Type III tests)
## 
## Response: visit
##              Sum Sq Df F value Pr(>F)
## (Intercept)    55.9  1  0.0231 0.8821
## fee            13.6  1  0.0056 0.9416
## ad           1625.8  1  0.6702 0.4303
## interaction   350.2  1  0.1444 0.7112
## Residuals   26683.1 11

code 13.13

install.packages(“leaps”)

null<-lm(visit ~ 1, data=sale)
full<-lm(visit ~. , data=sale) 

step(null, scope=list(lower=null, upper=full), direction="forward")
## Start:  AIC=125.51
## visit ~ 1
## 
##                      Df Sum of Sq   RSS     AIC
## + visit.center        1     56493     0 -928.96
## + std.visit           1     56493     0 -928.40
## + ad                  1     17484 39009  121.95
## + ad.center           1     17484 39009  121.95
## + std.ad              1     17484 39009  121.95
## + db                  1     14963 41530  122.89
## + fee                 1     11100 45393  124.23
## + fee.center          1     11100 45393  124.23
## + std.fee             1     11100 45393  124.23
## + region              2     16173 40320  124.45
## <none>                            56493  125.51
## + da                  1       963 55530  127.25
## + std.interaction     1       574 55919  127.35
## + interaction         1         5 56489  127.51
## + interaction.center  1         5 56489  127.51
## 
## Step:  AIC=-928.96
## visit ~ visit.center
## Warning: attempting model selection on an essentially perfect fit is
## nonsense
##                      Df  Sum of Sq        RSS     AIC
## + da                  1 2.7882e-27 1.1803e-26 -930.14
## <none>                             1.4591e-26 -928.96
## + fee                 1 1.7264e-27 1.2864e-26 -928.85
## + fee.center          1 1.7264e-27 1.2864e-26 -928.85
## + std.fee             1 1.7264e-27 1.2864e-26 -928.85
## + region              2 3.0206e-27 1.1570e-26 -928.44
## + interaction         1 8.0819e-28 1.3783e-26 -927.82
## + interaction.center  1 8.0819e-28 1.3783e-26 -927.82
## + db                  1 1.8949e-28 1.4401e-26 -927.16
## + std.interaction     1 5.1960e-29 1.4539e-26 -927.02
## + ad                  1 5.8900e-30 1.4585e-26 -926.97
## + ad.center           1 5.8900e-30 1.4585e-26 -926.97
## + std.ad              1 5.8900e-30 1.4585e-26 -926.97
## 
## Step:  AIC=-930.14
## visit ~ visit.center + da
## Warning: attempting model selection on an essentially perfect fit is
## nonsense
##                      Df  Sum of Sq        RSS     AIC
## + fee                 1 3.7977e-27 8.0049e-27 -933.97
## + fee.center          1 3.7977e-27 8.0049e-27 -933.97
## + std.fee             1 3.7977e-27 8.0049e-27 -933.97
## + interaction         1 1.7011e-27 1.0101e-26 -930.48
## + interaction.center  1 1.7011e-27 1.0101e-26 -930.48
## <none>                             1.1803e-26 -930.14
## + region              1 2.3230e-28 1.1570e-26 -928.44
## + db                  1 2.3230e-28 1.1570e-26 -928.44
## + std.interaction     1 3.2000e-30 1.1799e-26 -928.15
## + ad                  1 1.8000e-30 1.1801e-26 -928.15
## + ad.center           1 1.8000e-30 1.1801e-26 -928.15
## + std.ad              1 1.8000e-30 1.1801e-26 -928.15
## 
## Step:  AIC=-933.97
## visit ~ visit.center + da + fee
## Warning: attempting model selection on an essentially perfect fit is
## nonsense
##                      Df  Sum of Sq        RSS     AIC
## <none>                             8.0049e-27 -933.97
## + ad                  1 6.4599e-28 7.3589e-27 -933.23
## + ad.center           1 6.4599e-28 7.3589e-27 -933.23
## + std.ad              1 6.4599e-28 7.3589e-27 -933.23
## + interaction         1 5.1376e-28 7.4911e-27 -932.96
## + interaction.center  1 5.1376e-28 7.4911e-27 -932.96
## + region              1 2.9585e-28 7.7090e-27 -932.53
## + db                  1 2.9585e-28 7.7090e-27 -932.53
## + std.interaction     1 3.2700e-30 8.0016e-27 -931.97
## 
## Call:
## lm(formula = visit ~ visit.center + da + fee, data = sale)
## 
## Coefficients:
##  (Intercept)  visit.center            da           fee  
##    3.993e+02     1.000e+00    -4.064e-14     1.655e-14
step(full, direction="backward")
## Start:  AIC=-931.76
## visit ~ fee + ad + interaction + region + da + db + visit.center + 
##     fee.center + ad.center + interaction.center + std.visit + 
##     std.fee + std.ad + std.interaction
## Warning: attempting model selection on an essentially perfect fit is
## nonsense
## 
## Step:  AIC=-931.76
## visit ~ fee + ad + interaction + region + da + db + visit.center + 
##     fee.center + ad.center + interaction.center + std.visit + 
##     std.fee + std.ad
## Warning: attempting model selection on an essentially perfect fit is
## nonsense
## 
## Step:  AIC=-931.76
## visit ~ fee + ad + interaction + region + da + db + visit.center + 
##     fee.center + ad.center + interaction.center + std.visit + 
##     std.fee
## Warning: attempting model selection on an essentially perfect fit is
## nonsense
## 
## Step:  AIC=-931.76
## visit ~ fee + ad + interaction + region + da + db + visit.center + 
##     fee.center + ad.center + interaction.center + std.visit
## Warning: attempting model selection on an essentially perfect fit is
## nonsense
## 
## Step:  AIC=-931.76
## visit ~ fee + ad + interaction + region + da + db + visit.center + 
##     fee.center + ad.center + interaction.center
## Warning: attempting model selection on an essentially perfect fit is
## nonsense
## 
## Step:  AIC=-931.76
## visit ~ fee + ad + interaction + region + da + db + visit.center + 
##     fee.center + ad.center
## Warning: attempting model selection on an essentially perfect fit is
## nonsense
## 
## Step:  AIC=-931.76
## visit ~ fee + ad + interaction + region + da + db + visit.center + 
##     fee.center
## Warning: attempting model selection on an essentially perfect fit is
## nonsense
## 
## Step:  AIC=-931.76
## visit ~ fee + ad + interaction + region + da + db + visit.center
## Warning: attempting model selection on an essentially perfect fit is
## nonsense
## 
## Step:  AIC=-931.76
## visit ~ fee + ad + interaction + region + da + visit.center
## Warning: attempting model selection on an essentially perfect fit is
## nonsense
## 
## Step:  AIC=-931.76
## visit ~ fee + ad + interaction + region + visit.center
## Warning: attempting model selection on an essentially perfect fit is
## nonsense
##                Df Sum of Sq   RSS     AIC
## - interaction   1         0     0 -934.95
## <none>                          0 -931.76
## - region        2         0     0 -928.05
## - ad            1         0     0 -926.00
## - fee           1         0     0 -925.82
## - visit.center  1     19822 19822  119.80
## 
## Step:  AIC=-934.95
## visit ~ fee + ad + region + visit.center
## Warning: attempting model selection on an essentially perfect fit is
## nonsense
##                Df Sum of Sq   RSS     AIC
## <none>                          0 -934.95
## - region        2         0     0 -930.24
## - fee           1         0     0 -927.41
## - ad            1         0     0 -919.80
## - visit.center  1     21140 21140  118.76
## 
## Call:
## lm(formula = visit ~ fee + ad + region + visit.center, data = sale)
## 
## Coefficients:
##  (Intercept)           fee            ad       regionB       regionC  
##    3.993e+02     2.773e-14    -2.946e-14     3.199e-14     6.441e-14  
## visit.center  
##    1.000e+00
step(null, scope = list(upper=full), direction="both")
## Start:  AIC=125.51
## visit ~ 1
## 
##                      Df Sum of Sq   RSS     AIC
## + visit.center        1     56493     0 -928.96
## + std.visit           1     56493     0 -928.40
## + ad                  1     17484 39009  121.95
## + ad.center           1     17484 39009  121.95
## + std.ad              1     17484 39009  121.95
## + db                  1     14963 41530  122.89
## + fee                 1     11100 45393  124.23
## + fee.center          1     11100 45393  124.23
## + std.fee             1     11100 45393  124.23
## + region              2     16173 40320  124.45
## <none>                            56493  125.51
## + da                  1       963 55530  127.25
## + std.interaction     1       574 55919  127.35
## + interaction         1         5 56489  127.51
## + interaction.center  1         5 56489  127.51
## 
## Step:  AIC=-928.96
## visit ~ visit.center
## Warning: attempting model selection on an essentially perfect fit is
## nonsense
## Warning: attempting model selection on an essentially perfect fit is
## nonsense
##                      Df Sum of Sq   RSS     AIC
## + da                  1         0     0 -930.14
## <none>                                0 -928.96
## + fee                 1         0     0 -928.85
## + fee.center          1         0     0 -928.85
## + std.fee             1         0     0 -928.85
## + region              2         0     0 -928.44
## + interaction         1         0     0 -927.82
## + interaction.center  1         0     0 -927.82
## + db                  1         0     0 -927.16
## + std.interaction     1         0     0 -927.02
## + ad                  1         0     0 -926.97
## + ad.center           1         0     0 -926.97
## + std.ad              1         0     0 -926.97
## - visit.center        1     56493 56493  125.51
## 
## Step:  AIC=-930.14
## visit ~ visit.center + da
## Warning: attempting model selection on an essentially perfect fit is
## nonsense
## Warning: attempting model selection on an essentially perfect fit is
## nonsense
##                      Df Sum of Sq   RSS     AIC
## + fee                 1         0     0 -933.97
## + fee.center          1         0     0 -933.97
## + std.fee             1         0     0 -933.97
## + interaction         1         0     0 -930.48
## + interaction.center  1         0     0 -930.48
## <none>                                0 -930.14
## - da                  1         0     0 -928.96
## + region              1         0     0 -928.44
## + db                  1         0     0 -928.44
## + std.interaction     1         0     0 -928.15
## + ad                  1         0     0 -928.15
## + ad.center           1         0     0 -928.15
## + std.ad              1         0     0 -928.15
## - visit.center        1     55530 55530  127.25
## 
## Step:  AIC=-933.97
## visit ~ visit.center + da + fee
## Warning: attempting model selection on an essentially perfect fit is
## nonsense
## Warning: attempting model selection on an essentially perfect fit is
## nonsense
##                      Df Sum of Sq   RSS     AIC
## <none>                                0 -933.97
## + ad                  1         0     0 -933.23
## + ad.center           1         0     0 -933.23
## + std.ad              1         0     0 -933.23
## + interaction         1         0     0 -932.96
## + interaction.center  1         0     0 -932.96
## + region              1         0     0 -932.53
## + db                  1         0     0 -932.53
## + std.interaction     1         0     0 -931.97
## - fee                 1         0     0 -930.14
## - da                  1         0     0 -928.85
## - visit.center        1     45365 45365  126.22
## 
## Call:
## lm(formula = visit ~ visit.center + da + fee, data = sale)
## 
## Coefficients:
##  (Intercept)  visit.center            da           fee  
##    3.993e+02     1.000e+00    -4.064e-14     1.655e-14
library(leaps)

test<-regsubsets(visit ~ fee + ad + interaction, data=sale)

summary(test)
## Subset selection object
## Call: regsubsets.formula(visit ~ fee + ad + interaction, data = sale)
## 3 Variables  (and intercept)
##             Forced in Forced out
## fee             FALSE      FALSE
## ad              FALSE      FALSE
## interaction     FALSE      FALSE
## 1 subsets of each size up to 3
## Selection Algorithm: exhaustive
##          fee ad  interaction
## 1  ( 1 ) " " "*" " "        
## 2  ( 1 ) " " "*" "*"        
## 3  ( 1 ) "*" "*" "*"
plot(test, scale="bic")

plot(test, scale="adjr2")

plot(test, scale="r2")