> # Tomato exp't > y=scan("http://www.stat.ucla.edu/~hqxu/stat225/data/tomato.dat") # scan or type data > y <- c(16.1, 15.3, 17.5, 16.6, 19.2, 18.5, 20.8, 18.0, 21.0, 8.1, 8.6, 10.1, 12.7, 13.7, 11.5, 14.4, 15.4, 13.7) > tom2 <- rep(c(0,1), c(9,9)) > denl.tom1 <- rep(c(-1,0,1,0,0,0), rep(3,6))/sqrt(2) > denl.tom2 <- rep(c(0,0,0,-1,0,1), rep(3,6))/sqrt(2) > denq.tom1 <- rep(c(1,-2,1,0,0,0), rep(3,6))/sqrt(6) > denq.tom2 <- rep(c(0,0,0,1,-2,1), rep(3,6))/sqrt(6) > > # nested model > g<- lm(y ~ tom2 + denl.tom1 + denl.tom2 + denq.tom1 + denq.tom2) > summary(g) Call: lm(formula = y ~ tom2 + denl.tom1 + denl.tom2 + denq.tom1 + denq.tom2) Residuals: Min 1Q Median 3Q Max -1.93333 -0.82500 -0.01667 1.02500 1.20000 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) 18.11111 0.40575 44.637 1.04e-14 *** tom2 -6.08889 0.57381 -10.611 1.88e-07 *** denl.tom1 2.56915 0.70277 3.656 0.003292 ** denl.tom2 3.93623 0.70277 5.601 0.000116 *** denq.tom1 0.01361 0.70277 0.019 0.984869 denq.tom2 -0.74846 0.70277 -1.065 0.307835 --- Signif. codes: 0 `***' 0.001 `**' 0.01 `*' 0.05 `.' 0.1 ` ' 1 Residual standard error: 1.217 on 12 degrees of freedom Multiple R-Squared: 0.9296, Adjusted R-squared: 0.9003 F-statistic: 31.69 on 5 and 12 DF, p-value: 1.625e-06 > # drop the two quadratic terms > g<- lm(y ~ tom2 + denl.tom1 + denl.tom2 ) > summary(g) Call: lm(formula = y ~ tom2 + denl.tom1 + denl.tom2) Residuals: Min 1Q Median 3Q Max -1.92778 -0.90556 0.09722 0.86944 1.67778 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) 18.1111 0.3930 46.084 < 2e-16 *** tom2 -6.0889 0.5558 -10.955 2.98e-08 *** denl.tom1 2.5692 0.6807 3.774 0.00205 ** denl.tom2 3.9362 0.6807 5.783 4.75e-05 *** --- Signif. codes: 0 `***' 0.001 `**' 0.01 `*' 0.05 `.' 0.1 ` ' 1 Residual standard error: 1.179 on 14 degrees of freedom Multiple R-Squared: 0.923, Adjusted R-squared: 0.9064 F-statistic: 55.9 on 3 and 14 DF, p-value: 4.891e-08 > > # final model, need a new variable for the linear effect > denl <- rep(c(-1,0,1,-1,0,1), rep(3,6))/sqrt(2) > g<- lm(y ~ tom2 + denl ) > summary(g) Call: lm(formula = y ~ tom2 + denl) Residuals: Min 1Q Median 3Q Max -2.4111 -0.5972 0.3333 0.6556 1.6889 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) 18.1111 0.4061 44.60 < 2e-16 *** tom2 -6.0889 0.5743 -10.60 2.30e-08 *** denl 3.2527 0.4974 6.54 9.36e-06 *** --- Signif. codes: 0 `***' 0.001 `**' 0.01 `*' 0.05 `.' 0.1 ` ' 1 Residual standard error: 1.218 on 15 degrees of freedom Multiple R-Squared: 0.9119, Adjusted R-squared: 0.9001 F-statistic: 77.58 on 2 and 15 DF, p-value: 1.228e-08 > > # an easy way > tomato <- as.factor( rep( c(0,1), c(9,9))) > density <- as.ordered( rep(rep( c(1,2,3), c(3,3,3)), 2)) > > g<-lm(y~tomato/density) > summary(g) Call: lm(formula = y ~ tomato/density) Residuals: Min 1Q Median 3Q Max -1.93333 -0.82500 -0.01667 1.02500 1.20000 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) 18.11111 0.40575 44.637 1.04e-14 *** tomato1 -6.08889 0.57381 -10.611 1.88e-07 *** tomato0:density.L 2.56915 0.70277 3.656 0.003292 ** tomato1:density.L 3.93623 0.70277 5.601 0.000116 *** tomato0:density.Q 0.01361 0.70277 0.019 0.984869 tomato1:density.Q -0.74846 0.70277 -1.065 0.307835 --- Signif. codes: 0 `***' 0.001 `**' 0.01 `*' 0.05 `.' 0.1 ` ' 1 Residual standard error: 1.217 on 12 degrees of freedom Multiple R-Squared: 0.9296, Adjusted R-squared: 0.9003 F-statistic: 31.69 on 5 and 12 DF, p-value: 1.625e-06 > > g<-lm(y~tomato+density) > summary(g) Call: lm(formula = y ~ tomato + density) Residuals: Min 1Q Median 3Q Max -2.2611 -0.7347 0.3028 0.6889 1.8389 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) 18.1111 0.4126 43.891 2.22e-16 *** tomato1 -6.0889 0.5836 -10.434 5.51e-08 *** density.L 3.2527 0.5054 6.436 1.56e-05 *** density.Q -0.3674 0.5054 -0.727 0.479 --- Signif. codes: 0 `***' 0.001 `**' 0.01 `*' 0.05 `.' 0.1 ` ' 1 Residual standard error: 1.238 on 14 degrees of freedom Multiple R-Squared: 0.9151, Adjusted R-squared: 0.8969 F-statistic: 50.27 on 3 and 14 DF, p-value: 9.644e-08 > > # compare with separate fitting, same estimates but different s.e. > g<-lm(y~density, subset=tomato==0) > summary(g) Call: lm(formula = y ~ density, subset = tomato == 0) Residuals: Min 1Q Median 3Q Max -1.933 -1.000 0.400 1.067 1.200 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) 18.11111 0.46600 38.865 1.94e-08 *** density.L 2.56915 0.80714 3.183 0.019 * density.Q 0.01361 0.80714 0.017 0.987 --- Signif. codes: 0 `***' 0.001 `**' 0.01 `*' 0.05 `.' 0.1 ` ' 1 Residual standard error: 1.398 on 6 degrees of freedom Multiple R-Squared: 0.6281, Adjusted R-squared: 0.5041 F-statistic: 5.066 on 2 and 6 DF, p-value: 0.05145 > g<-lm(y~density, subset=tomato==1) > summary(g) Call: lm(formula = y ~ density, subset = tomato == 1) Residuals: Min 1Q Median 3Q Max -1.133 -0.800 -0.100 0.900 1.167 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) 12.0222 0.3348 35.907 3.11e-08 *** density.L 3.9362 0.5799 6.788 0.0005 *** density.Q -0.7485 0.5799 -1.291 0.2443 --- Signif. codes: 0 `***' 0.001 `**' 0.01 `*' 0.05 `.' 0.1 ` ' 1 Residual standard error: 1.004 on 6 degrees of freedom Multiple R-Squared: 0.8883, Adjusted R-squared: 0.8511 F-statistic: 23.87 on 2 and 6 DF, p-value: 0.001392