How to wrap RHS terms of a formula with a function

IRTFM

I can construct a formula that does what I desire starting with the character versions of terms in a formula, but I'm stumbling in starting with a formula object:

form1 <- Y ~ A + B 
form1[-c(1,2)][[1]]
#A + B

Now how to build a formula object that looks like:

 Y ~ poly(A, 2) + poly(B, 2) + poly(C, 2)

Or:

 Y ~ pspline(A, 4) + pspline(B, 4) + pspline(C, 4)

Seems that it might involve a recursive walk along the RHS but I'm not getting progress. It just occurred to me that I might use

> attr( terms(form1), "term.labels")
[1] "A" "B"

And then use the as.formula(character-expr) approach, but I's sorly of like to see an lapply (RHS_form, somefunc) version of a polyize (or perhaps polymer?) function.

MrFlick

If I borrow some functions I originally wrote here, you could do something like this. First, the helper functions...

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))))))
}

And then you can use

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)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to capture the fixed terms of a R formula?

From Dev

How to wrap the jQuery function

From Dev

How to wrap function in haskell?

From Dev

How to wrap function in haskell?

From Dev

How to drop some interaction terms from R formula

From Dev

How to drop some interaction terms from R formula

From Dev

Remove variable from RHS of a formula that has a dot

From Dev

How to wrap printf() into a function or macro?

From Dev

Magic sprintf function - how to wrap it?

From Dev

How to wrap an asynchronous function in a promise?

From Dev

Formula from list of quoted terms

From Dev

Formula from list of quoted terms

From Dev

How to wrap a function in a function to avoid code repetition

From Dev

How to write formula with function inside a function

From Dev

How to convert formula to function, or apply the formula to some values?

From Dev

How to extract independent variable names from a formula with interaction and high order terms?

From Dev

How to write a bash function to wrap another command?

From Dev

How to wrap "descending sort in data table" into a function?

From Dev

How to wrap function that takes unlimited number of arguments

From Dev

In RVCT Compile Environment, how to wrap a function

From Dev

How to wrap ncurses's printw function?

From Dev

How to use SWIG to wrap std::function objects?

From Dev

How to use original parameter in `_.wrap` function in Underscore

From Dev

How to work with stl with functor wrap of function pointer?

From Dev

In RVCT Compile Environment, how to wrap a function

From Dev

How to wrap JSON to a javascript function call

From Dev

How to evaluate a spreadsheet formula within a custom function?

From Dev

How to split this formula into function in c++?

From Dev

How to specify subjective columns for a formula within a function

Related Related

  1. 1

    How to capture the fixed terms of a R formula?

  2. 2

    How to wrap the jQuery function

  3. 3

    How to wrap function in haskell?

  4. 4

    How to wrap function in haskell?

  5. 5

    How to drop some interaction terms from R formula

  6. 6

    How to drop some interaction terms from R formula

  7. 7

    Remove variable from RHS of a formula that has a dot

  8. 8

    How to wrap printf() into a function or macro?

  9. 9

    Magic sprintf function - how to wrap it?

  10. 10

    How to wrap an asynchronous function in a promise?

  11. 11

    Formula from list of quoted terms

  12. 12

    Formula from list of quoted terms

  13. 13

    How to wrap a function in a function to avoid code repetition

  14. 14

    How to write formula with function inside a function

  15. 15

    How to convert formula to function, or apply the formula to some values?

  16. 16

    How to extract independent variable names from a formula with interaction and high order terms?

  17. 17

    How to write a bash function to wrap another command?

  18. 18

    How to wrap "descending sort in data table" into a function?

  19. 19

    How to wrap function that takes unlimited number of arguments

  20. 20

    In RVCT Compile Environment, how to wrap a function

  21. 21

    How to wrap ncurses's printw function?

  22. 22

    How to use SWIG to wrap std::function objects?

  23. 23

    How to use original parameter in `_.wrap` function in Underscore

  24. 24

    How to work with stl with functor wrap of function pointer?

  25. 25

    In RVCT Compile Environment, how to wrap a function

  26. 26

    How to wrap JSON to a javascript function call

  27. 27

    How to evaluate a spreadsheet formula within a custom function?

  28. 28

    How to split this formula into function in c++?

  29. 29

    How to specify subjective columns for a formula within a function

HotTag

Archive