R语言学习笔记——合并两个公式

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)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值