作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我可以构建一个公式,从公式中术语的字符版本开始,我想要做什么,但我在从公式对象开始时遇到了困难:
form1 <- Y ~ A + B
form1[-c(1,2)][[1]]
#A + B
Y ~ poly(A, 2) + poly(B, 2) + poly(C, 2)
Y ~ pspline(A, 4) + pspline(B, 4) + pspline(C, 4)
> attr( terms(form1), "term.labels")
[1] "A" "B"
as.formula
(character-expr) 方法,但我非常喜欢看到
lapply (RHS_form, somefunc)
polyize
的版本(或者
polymer
?)函数。
最佳答案
如果借用我原来写的一些函数here ,你可以做这样的事情。首先,辅助函数...
extract_rhs_symbols <- function(x) {
as.list(attr(delete.response(terms(x)), "variables"))[-1]
}
symbols_to_formula <- function(x) {
as.call(list(quote(`~`), x))
}
sum_symbols <- function(...) {
Reduce(function(a,b) bquote(.(a)+.(b)), do.call(`c`, list(...), quote=T))
}
transform_terms <- function(x, f) {
symbols_to_formula(sum_symbols(sapply(extract_rhs_symbols(x), function(x) do.call("substitute",list(f, list(x=x))))))
}
update(form1, transform_terms(form1, quote(poly(x, 2))))
# Y ~ poly(A, 2) + poly(B, 2)
update(form1, transform_terms(form1, quote(pspline(x, 4))))
# Y ~ pspline(A, 4) + pspline(B, 4)
关于r - 如何用函数包装公式的 RHS 项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35829487/
我是一名优秀的程序员,十分优秀!