(一)
https://stackoverflow.com/questions/4951442/formula-with-dynamic-number-of-variables
for (i in seq_len(factor_number)) { for (j in seq(i + 1, factor_number)) { linear_model <- lm(Y ~ F1 + F2, list(Y=foo_data_frame$Y, F1=foo_data_frame[[i]], F2=foo_data_frame[[j]])) # linear_model further analyzing... } }
See ?as.formula
, e.g.:
factors <- c("factor1", "factor2") as.formula(paste("y~", paste(factors, collapse="+"))) # y ~ factor1 + factor2
where factors
is a character vector containing the names of the factors you want to use in the model. This you can paste into an lm
model, e.g.:
set.seed(0) y <- rnorm(100) factor1 <- rep(1:2, each=50) factor2 <- rep(3:4, 50) lm(as.formula(paste("y~", paste(factors, collapse="+")))) # Call: # lm(formula = as.formula(paste("y~", paste(factors, collapse = "+")))) # Coefficients: # (Intercept) factor1 factor2 # 0.542471 -0.002525 -0.147433
(二)
> model4 <- lm(LungCapData[1:10, 1] ~ LungCapData[1:10, 2] + LungCapData[1:10, 3] + LungCapData[1:10, 4] + LungCapData[1:10, 5]) > summary(model4) Call: lm(formula = LungCapData[1:10, 1] ~ LungCapData[1:10, 2] + LungCapData[1:10, 3] + LungCapData[1:10, 4] + LungCapData[1:10, 5]) Residuals: 1 2 3 4 5 6 7 8 9 1.006e+00 8.327e-17 1.162e-01 1.773e+00 3.168e-01 -1.162e-01 -1.364e+00 -9.059e-01 -7.653e-01 10 -6.031e-02 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) -4.6843 6.3616 -0.736 0.495 LungCapData[1:10, 2] 0.3493 0.2142 1.630 0.164 LungCapData[1:10, 3] 0.1224 0.1317 0.930 0.395 LungCapData[1:10, 4]yes -0.6192 1.7090 -0.362 0.732 LungCapData[1:10, 5]male 0.4579 1.2773 0.358 0.735 Residual standard error: 1.229 on 5 degrees of freedom Multiple R-squared: 0.8242, Adjusted R-squared: 0.6835 F-statistic: 5.859 on 4 and 5 DF, p-value: 0.03968 > > model5 <- lm(LungCapData[1:725, 1] ~ LungCapData[1:725, 2] + LungCapData[1:725, 3] + LungCapData[1:725, 4] + LungCapData[1:725, 5]) > summary(model5) Call: lm(formula = LungCapData[1:725, 1] ~ LungCapData[1:725, 2] + LungCapData[1:725, 3] + LungCapData[1:725, 4] + LungCapData[1:725, 5]) Residuals: Min 1Q Median 3Q Max -3.2915 -0.7360 0.0184 0.7125 3.0599 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) -11.33282 0.47245 -23.987 < 2e-16 *** LungCapData[1:725, 2] 0.16012 0.01806 8.864 < 2e-16 *** LungCapData[1:725, 3] 0.26363 0.01009 26.123 < 2e-16 *** LungCapData[1:725, 4]yes -0.61774 0.12633 -4.890 1.24e-06 *** LungCapData[1:725, 5]male 0.38528 0.07991 4.822 1.74e-06 *** --- Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 Residual standard error: 1.023 on 720 degrees of freedom Multiple R-squared: 0.8531, Adjusted R-squared: 0.8523 F-statistic: 1045 on 4 and 720 DF, p-value: < 2.2e-16 > model0 <- lm(LungCap ~ Age + Height + Smoke + Gender) > summary(model0) Call: lm(formula = LungCap ~ Age + Height + Smoke + Gender) Residuals: Min 1Q Median 3Q Max -3.2915 -0.7360 0.0184 0.7125 3.0599 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) -11.33282 0.47245 -23.987 < 2e-16 *** Age 0.16012 0.01806 8.864 < 2e-16 *** Height 0.26363 0.01009 26.123 < 2e-16 *** Smokeyes -0.61774 0.12633 -4.890 1.24e-06 *** Gendermale 0.38528 0.07991 4.822 1.74e-06 *** --- Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 Residual standard error: 1.023 on 720 degrees of freedom Multiple R-squared: 0.8531, Adjusted R-squared: 0.8523 F-statistic: 1045 on 4 and 720 DF, p-value: < 2.2e-16
(三)
http://www.cnblogs.com/howlowl/p/8512222.html
> mod <- lm(LungCap ~ Age) > mod Call: lm(formula = LungCap ~ Age) Coefficients: (Intercept) Age 1.1469 0.5448 > summary(mod) Call: lm(formula = LungCap ~ Age) Residuals: Min 1Q Median 3Q Max -4.7799 -1.0203 -0.0005 0.9789 4.2650 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) 1.14686 0.18353 6.249 7.06e-10 *** Age 0.54485 0.01416 38.476 < 2e-16 *** --- Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 Residual standard error: 1.526 on 723 degrees of freedom Multiple R-squared: 0.6719, Adjusted R-squared: 0.6714 F-statistic: 1480 on 1 and 723 DF, p-value: < 2.2e-16 > attributes(summary(mod)) $names [1] "call" "terms" "residuals" "coefficients" "aliased" "sigma" [7] "df" "r.squared" "adj.r.squared" "fstatistic" "cov.unscaled" $class [1] "summary.lm" > summary(mod)$r.squared [1] 0.6718669 > summary(mod)$adj.r.squared [1] 0.6714131 > summary(mod)$coefficients[, 4] (Intercept) Age 7.056380e-10 4.077172e-177 > summary(mod)$coefficients[,1:4] Estimate Std. Error t value Pr(>|t|) (Intercept) 1.1468578 0.18352850 6.248936 7.056380e-10 Age 0.5448484 0.01416087 38.475634 4.077172e-177 > class(summary(mod)) [1] "summary.lm" > class(summary(mod)$coefficients) [1] "matrix" > class(summary(mod)$coefficients[,4]) [1] "numeric" > class(summary(mod)$coefficients[,1:4]) [1] "matrix" > > summary(mod)$coefficients[, 4]["(Intercept)"] (Intercept) 7.05638e-10 > summary(mod)$coefficients[, 4]["Age"] Age 4.077172e-177 > > summary(mod)$coefficients[, 4][1] (Intercept) 7.05638e-10 > summary(mod)$coefficients[, 4][2] Age 4.077172e-177 > summary(mod)$coefficients[, 4][3] <NA> NA
(四)append values to vector in r
Here are several ways to do it. All of them are discouraged. Appending to an object in a for loop causes the entire object to be copied on every iteration, which causes a lot of people to say "R is slow", or "R loops should be avoided".
# one way for (i in 1:length(values)) vector[i] <- values[i] # another way for (i in 1:length(values)) vector <- c(vector, values[i]) # yet another way?!? for (v in values) vector <- c(vector, v) # ... more ways
help("append")
would have answered your question and saved the time it took you to write this question (but would have caused you to develop bad habits). ;-)
Note that vector <- c()
isn‘t an empty vector; it‘s NULL
. If you want an empty character vector, use vector <- character()
.
Also note, as BrodieG pointed out in the comments: if you absolutely must use a for loop, then at least pre-allocate the entire vector before the loop. This will be much faster than appending for larger vectors.
set.seed(21) values <- sample(letters, 1e4, TRUE) vector <- character(0) # slow system.time( for (i in 1:length(values)) vector[i] <- values[i] ) # user system elapsed # 0.340 0.000 0.343 vector <- character(length(values)) # fast(er) system.time( for (i in 1:length(values)) vector[i] <- values[i] ) # user system elapsed # 0.024 0.000 0.023