R formula of type y ~ fn(type="x")+fn(type="z"), disregard the second occurence of fn? -
I have a model building function where the formula may contain some functions, and I would like to do this so that user input Function is used many times, only for the first time with a warning, for example This works because the function < Code> model.frame is used in the code> word , but in my case I am working with the work inside the formula which is not necessarily the same logic List, and I would like to extend this behavior so that the logic of these functions does not make any difference: Here is an example function (highly simplified) I used above: I check the name of special (function call) Can I change the names like them before the event, but I was thinking that there would be a more clever way of dealing with this? I was not able to work your code correctly, but I believe that I understood your work Maybe, something that fulfills what you do after 're'. LM if we use the same variable twice, then the second one is released: < Pre>
y & lt; -1: 3 x
model (y ~ x + fn ("x")) (interrupt) x Temp 1 1 1 1 2 1 2 1 3 1 3 1 model (y ~ x + fn ("x") + fn ("x") # Directional function calls (intercept) X temporary 1 1 1 1 2 1 2 1 3 1 3 1 model (y_x + fn ("x") + fn ("z")) with # different argument value error in # function atrate (all_terms, "variables") [[1 + ind_fn]]: Out of the CSS limit
model and lieutenant; - Function (formula, data) {Too much LM function from the beginning MF & lt; - match.call (expand.dots = FALSE) mf & lt; - MF [C (1L, Match (C ("Formula", "Data"), Name (MF), 0L)) MF [[1L]]
- as.name ("model.frame") mf $ na.action & lt; - (as the name ("na.pass") all_terms & lt; - if (unavailable (data)) rules (formula, "Fn")} other rules (formula, "FN", data = data) # Identification Update the formula by removing the part of the function ind_fn & lt; - attr (all_terms, "specials") $ fn "fn" section (! Is.null (ind_fn)) {fn_term & lt; - attr (All_terms, "Variables") [[1 + ind_fn]] threads - update (formula, paste (". ~ .-", deparse (fn_term, width.cutoff = 500L, backtick = TRUE))) mf $ formula & lt ; -formula} # build y and x mf & lt; - eval (mf, parent.frame ()) - Model reaction (mf, "numeric") MT & Lt; - etr (mf, "no Yum ") X-Lt.-Model.Matrix (MT, MF) #if, if in the formula, do something with it (! Null (ind_fn)) {foobar & lt; -function (type = c (" x " "," Z ")) {if (type ==" x ") {rep (1, nrow (x)}} representative (0, n (x)) fn_term [[1]]
f < - y ~ x + fn ("x") + fn ("z") + z + fn ('a') # Get a list of words wars & lt; - as.list (attr (rank ('f', 'variables')) Obtain the conditions which may lead to duplicate calls & lt; - wars [sapply (wars, is.call) & amp; Duplicate (Spey (wars, function (x) agl list (x) [[1]])] # Remove duplicate calls with formula update (f, paste (". ~.", Paste (sapply (unnecessary, # Y ~ X + fn ("x") + z , fall = '-'), Sep = '-'))
Comments
Post a Comment