> for (i in 1:5) + assign(paste("fred", i, sep=""), i) > ls() [1] "fred1" "fred2" "fred3" "fred4" "fred5" "i" > "fred3" <- 14 > fred3 [1] 14 > > for (i in 1:5) + paste("fred", i, sep="") <- i^2 Error: Target of assignment expands to non-language object > n <- 50 > x <- rnorm(n) > y <- x + rnorm(n) > out <- lm(y ~ poly(x,2 + ) + ) > summary(out) Call: lm(formula = y ~ poly(x, 2)) Residuals: Min 1Q Median 3Q Max -1.67504 -0.60217 -0.04372 0.63918 1.95124 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) -0.3845 0.1344 -2.862 0.00626 ** poly(x, 2)1 6.6454 0.9500 6.995 8.32e-09 *** poly(x, 2)2 1.5990 0.9500 1.683 0.09897 . --- Signif. codes: 0 `***' 0.001 `**' 0.01 `*' 0.05 `.' 0.1 ` ' 1 Residual standard error: 0.95 on 47 degrees of freedom Multiple R-Squared: 0.5241, Adjusted R-squared: 0.5039 F-statistic: 25.88 on 2 and 47 DF, p-value: 2.638e-08 > out.too <- lm(y ~ x + I(x^2)) > summary(out.too) Call: lm(formula = y ~ x + I(x^2)) Residuals: Min 1Q Median 3Q Max -1.67504 -0.60217 -0.04372 0.63918 1.95124 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) -0.2921 0.1718 -1.701 0.0956 . x 1.0227 0.1453 7.038 7.16e-09 *** I(x^2) 0.1850 0.1099 1.683 0.0990 . --- Signif. codes: 0 `***' 0.001 `**' 0.01 `*' 0.05 `.' 0.1 ` ' 1 Residual standard error: 0.95 on 47 degrees of freedom Multiple R-Squared: 0.5241, Adjusted R-squared: 0.5039 F-statistic: 25.88 on 2 and 47 DF, p-value: 2.638e-08 > all(out$fitted.values == out.too$fitted.values) [1] FALSE > max(abs(out$fitted.values == out.too$fitted.values)) [1] 1 > plot(x, y) > points(x, out$fitted.values, pch = "A", col = "red") > points(x, out.too$fitted.values, pch = "B", col = "green") > abline(0, 1, col = "blue") ### How embarrassing! > > max(abs(out$fitted.values - out.too$fitted.values)) [1] 1.110223e-15 > .Machine$double.eps [1] 2.220446e-16 > fred <- sample(c("red", "blue", "green"), n, replace = TRUE) > out.three <- lm(y ~ x + fred) Error in model.frame(formula, rownames, variables, varnames, extras, extranames, : invalid variable type > fred <- as.factor(fred) > fred [1] red red red red green red green blue blue green blue green [13] red blue blue blue green red blue green blue red red green [25] green blue blue red blue red green red red red red blue [37] red blue blue red green green blue blue red blue blue green [49] blue blue Levels: blue green red > as.numeric(fred) [1] 3 3 3 3 2 3 2 1 1 2 1 2 3 1 1 1 2 3 1 2 1 3 3 2 2 1 1 3 1 3 2 3 3 3 3 1 3 1[39] 1 3 2 2 1 1 3 1 1 2 1 1 > class(fred) [1] "factor" > out.three <- lm(y ~ x + fred) > summary(out.three) Call: lm(formula = y ~ x + fred) Residuals: Min 1Q Median 3Q Max -1.60144 -0.70271 -0.08877 0.62550 2.00764 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) -0.2591 0.2215 -1.170 0.248 x 1.0063 0.1501 6.705 2.52e-08 *** fredgreen 0.3828 0.3571 1.072 0.289 fredred 0.1150 0.3178 0.362 0.719 --- Signif. codes: 0 `***' 0.001 `**' 0.01 `*' 0.05 `.' 0.1 ` ' 1 Residual standard error: 0.9766 on 46 degrees of freedom Multiple R-Squared: 0.5078, Adjusted R-squared: 0.4757 F-statistic: 15.82 on 3 and 46 DF, p-value: 3.315e-07 > out.four <- lm(y ~ x) > summary(out.four) Call: lm(formula = y ~ x) Residuals: Min 1Q Median 3Q Max -1.7441 -0.6076 -0.1101 0.6478 1.9991 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) -0.1233 0.1421 -0.868 0.39 x 1.0161 0.1480 6.865 1.18e-08 *** --- Signif. codes: 0 `***' 0.001 `**' 0.01 `*' 0.05 `.' 0.1 ` ' 1 Residual standard error: 0.968 on 48 degrees of freedom Multiple R-Squared: 0.4954, Adjusted R-squared: 0.4849 F-statistic: 47.13 on 1 and 48 DF, p-value: 1.184e-08 > anova(out.four, out.three) Analysis of Variance Table Model 1: y ~ x Model 2: y ~ x + fred Res.Df RSS Df Sum of Sq F Pr(>F) 1 48 44.976 2 46 43.871 2 1.104 0.579 0.5645 #### bogus -- non-nested models > anova(out.too, out.three) Analysis of Variance Table Model 1: y ~ x + I(x^2) Model 2: y ~ x + fred Res.Df RSS Df Sum of Sq F Pr(>F) 1 47 42.419 2 46 43.871 1 -1.453 1.523 0.2234 > out.five <- lm(y ~ x * fred) > summary(out.five) Call: lm(formula = y ~ x * fred) Residuals: Min 1Q Median 3Q Max -1.4970 -0.7653 -0.1086 0.6384 2.3363 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) -0.3624 0.2340 -1.549 0.129 x 0.5865 0.3554 1.650 0.106 fredgreen 0.5106 0.3659 1.396 0.170 fredred 0.1771 0.3418 0.518 0.607 x:fredgreen 0.6257 0.4108 1.523 0.135 x:fredred 0.3060 0.4479 0.683 0.498 Residual standard error: 0.9706 on 44 degrees of freedom Multiple R-Squared: 0.535, Adjusted R-squared: 0.4821 F-statistic: 10.12 on 5 and 44 DF, p-value: 1.688e-06 > coefficients(out) (Intercept) poly(x, 2)1 poly(x, 2)2 -0.3845445 6.6453815 1.5990468 > coefficients(out.too) (Intercept) x I(x^2) -0.2921255 1.0227181 0.1850128 > out$coef (Intercept) poly(x, 2)1 poly(x, 2)2 -0.3845445 6.6453815 1.5990468 > formula(out.three) y ~ x + fred > predict(out.three) 1 2 3 4 5 6 0.70804131 -0.84577155 0.15528231 -1.27459016 2.63067005 -1.88201399 7 8 9 10 11 12 -0.07773475 0.18372314 -0.64209783 -0.30380187 -0.35966284 -1.94309225 13 14 15 16 17 18 -1.26895972 -0.95720804 -0.87267700 0.32385470 -1.00916765 -0.42513930 19 20 21 22 23 24 -0.35470826 1.03568669 -1.66224217 -0.89305743 -0.99591420 0.88774368 25 26 27 28 29 30 1.88037947 -1.02559143 0.23391712 -1.40096852 -1.18070273 -1.00793614 31 32 33 34 35 36 -0.93037260 0.15296724 1.10192752 -0.62904152 -1.40258006 0.44219649 37 38 39 40 41 42 0.43461273 -0.49522626 -0.81203166 0.74431206 -1.93478811 -0.66482949 43 44 45 46 47 48 -0.74941099 0.70464404 -0.41385790 -0.56062371 -0.13964738 0.47948214 49 50 -1.14702923 -1.06419085 > predict(out.three, newdata = data.frame(x c(1.1, 2.2), fred = c("red", "blue")) Error: syntax error > predict(out.three, newdata = data.frame(x = c(1.1, 2.2), fred = c("red", "blue")) + ) 1 2 0.9627785 1.9547398