Model.selection {biology} | R Documentation |
~~ A concise (1-5 lines) description of what the function does. ~~
Model.selection(object, data)
object |
~~Describe object here~~ |
data |
~~Describe data here~~ |
~~ If necessary, more details than the description above ~~
~Describe the value returned If it is a LIST, use
comp1 |
Description of 'comp1' |
comp2 |
Description of 'comp2' |
...
....
~~further notes~~
~Make other sections like Warning with section{Warning }{....} ~
~~who you are~~
~put references to the literature/web site here ~
~~objects to See Also as help
, ~~~
##---- Should be DIRECTLY executable !! ---- ##-- ==> Define data, use random, ##-- or do help(data=index) for the standard data sets. ## The function is currently defined as function (object, data) { library(hier.part) library(nlme) library(R.utils) predictors <- dimnames(attr(object$terms, "factors"))[[2]] comb <- combos(length(predictors))[[1]] ll <- length(comb[, 1]) var.names <- vector(mode = "character", length = ll) d <- data.frame(r.squared = 1:ll, Adj.r.sq = 1:ll, P = 1:ll, Cp = 1:ll, AIC = 1:ll, BIC = 1:ll, Select = factor(rep(" ", ll), levels = c(" ", "*"))) form <- object$call$formula minAIC = 1e+05 minBIC = 1e+05 minCp <- 1e+05 for (i in 1:length(comb[, 1])) { pred <- predictors[comb[i, ]] var.names[i] <- paste(pred, collapse = "+") ii <- paste(pred, collapse = "+", sep = "") new_fit <- update.formula(form, paste("~ +", ii)) fit <- lm(new_fit, data = data) fit_sum <- summary(lm(fit)) d$r.squared[i] <- fit_sum$r.squared d$Adj.r.sq[i] <- fit_sum$adj.r.squared d$P[i] <- fit_sum$fstatistic[["numdf"]] d$Cp[i] <- Cp(fit, object) deltaCp <- abs(d$Cp[i] - d$P[i]) d$AIC[i] <- extractAIC(fit)[2] if (minCp > d$Cp[i]) minCp <- d$Cp[i] if (minAIC > d$AIC[i]) minAIC <- d$AIC[i] d$BIC[i] <- extractAIC(fit, k = log(nrow(data)))[2] if (minBIC > d$BIC[i]) minBIC <- d$BIC[i] } rownames(d) <- var.names d$Select[d$BIC == minBIC] <- "*" d$Select[d$AIC == minAIC] <- "*" d$Select[d$Cp == minCp] <- "*" d }