form1<-y~x1+x2
form2<-y~x3+x4
merge.formula <- function(form1, form2, ...){
# get character strings of the names for the responses
# (i.e. left hand sides, lhs)
lhs1 <- deparse(form1[[2]])
lhs2 <- deparse(form2[[2]])
if(lhs1 != lhs2) stop('both formulas must have the same response')
# get character strings of the right hand sides
rhs1 <- strsplit(deparse(form1[[3]]), " \\+ ")[[1]]
rhs2 <- strsplit(deparse(form2[[3]]), " \\+ ")[[1]]
# create the merged rhs and lhs in character string form
rhs <- c(rhs1, rhs2)
lhs <- lhs1
# put the two sides together with the amazing
# reformulate function
out <- reformulate(rhs, lhs)
# set the environment of the formula (i.e. where should
# R look for variables when data aren't specified?)
environment(out) <- parent.frame()
return(out)
}
merge.formula(form2,form2)
###y ~ x3 + x4 + x3 + x4
##想合并两个公式,即合并“~”的内容,没有找到R包,如有更简单的方法欢迎交流。
add1():
Description
Compute all the single terms in the scope argument that can be added to or dropped from the model, fit those models and compute a table of the changes in fit.
Usage
add1(object, scope, …)
Default S3 method:
add1(object, scope, scale = 0, test = c(“none”, “Chisq”),
k = 2, trace = FALSE, …)
S3 method for class ‘lm’
add1(object, scope, scale = 0, test = c(“none”, “Chisq”, “F”),
x = NULL, k = 2, …)
S3 method for class ‘glm’
add1(object, scope, scale = 0,
test = c(“none”, “Rao”, “LRT”, “Chisq”, “F”),
x = NULL, k = 2, …)
drop1(object, scope, …)
Default S3 method:
drop1(object, scope, scale = 0, test = c(“none”, “Chisq”),
k = 2, trace = FALSE, …)
S3 method for class ‘lm’
drop1(object, scope, scale = 0, all.cols = TRUE,
test = c(“none”, “Chisq”, “F”), k = 2, …)
S3 method for class ‘glm’
drop1(object, scope, scale = 0,
test = c(“none”, “Rao”, “LRT”, “Chisq”, “F”),
k = 2, …)
lm1 <- lm(Fertility ~ ., data = swiss)
add1(lm1, ~ I(Education^2) + .^2)