Description Usage Arguments Details Value Note Author(s) See Also Examples
Apply function FUN
to each occurence of a call to what()
(or
a symbol what
) in an unevaluated expression. It can be used for advanced
manipulation of expressions.
Intended primarily for internal use.
1 
expr 
an unevaluated expression. 
what 
character string giving the name of a function. Each call to

FUN 
a function to be applied. 
symbols 
logical value controlling whether 
... 
optional arguments to 
FUN
is found by a call to match.fun
and can be either
a function or a symbol (e.g., a backquoted name) or a character string
specifying a function to be searched for from the environment of the call to
exprApply
.
A (modified) expression.
If expr
has a source reference information
("srcref"
attribute), modifications done by exprApply
will not be
visible when printed unless srcref
is removed. However, exprApply
does remove source reference from any function
expression inside
expr
.
Kamil Bartoń
Expressionrelated functions: substitute
,
expression
, quote
and bquote
.
Similar function walkCode
exists in package
codetools.
Functions useful inside FUN
: as.name
, as.call
,
call
, match.call
etc.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57  ### simple usage:
# print all Y(...) terms in a formula (note that symbol "Y" is omitted):
exprApply(~ X(1) + Y(2 + Y(4)) + N(Y + Y(3)), "Y", print)
# replace X() with log(X, base = n)
exprApply(expression(A() + B() + C()), c("A", "B", "C"), function(expr, base) {
expr[[2]] < expr[[1]]
expr[[1]] < as.name("log")
expr$base < base
expr
}, base = 10)
###
# TASK: fit lm with two poly terms, varying the degree from 1 to 3 in each.
# lm(y ~ poly(X1, degree = a) + poly(X2, degree = b), data = Cement)
# for a = {1,2,3} and b = {1,2,3}
# First we create a wrapper function for lm. Within it, use "exprApply" to add
# "degree" argument to all occurences of "poly()" having "X1" or "X2" as the
# first argument. Values for "degree" are taken from arguments "d1" and "d2"
lmpolywrap < function(formula, d1 = NA, d2 = NA, ...) {
cl < origCall < match.call()
cl[[1]] < as.name("lm")
cl$formula < exprApply(formula, "poly", function(e, degree, x) {
i < which(e[[2]] == x)[1]
if(!is.na(i) && !is.na(degree[i])) e$degree < degree[i]
e
}, degree = c(d1, d2), x = c("X1", "X2"))
cl$d1 < cl$d2 < NULL
fit < eval(cl, parent.frame())
fit$call < origCall # replace the stored call
fit
}
# global model:
fm < lmpolywrap(y ~ poly(X1) + poly(X2), data = Cement)
# Use "dredge" with argument "varying" to generate calls of all combinations of
# degrees for poly(X1) and poly(X2). Use "fixed = TRUE" to keep all global model
# terms in all models.
# Since "dredge" expects that global model has all the coefficients the
# submodels can have, which is not the case here, we first generate model calls,
# evaluate them and feed to "model.sel"
modCalls < dredge(fm,
varying = list(d1 = 1:3, d2 = 1:3),
fixed = TRUE,
evaluate = FALSE
)
model.sel(models < lapply(modCalls, eval))
# Note: to fit *all* submodels replace "fixed = TRUE" with:
# "subset = (d1==1  {poly(X1)}) && (d2==1  {poly(X2)})"
# This is to avoid fitting 3 identical models when the matching "poly()" term is
# absent.

Y(4)
Y(2 + Y(4))
Y(3)
~X(1) + Y(2 + Y(4)) + N(Y + Y(3))
expression(log(A, base = 10) + log(B, base = 10) + log(C, base = 10))
Fixed terms are "poly(X1)", "poly(X2)" and "(Intercept)"
Model selection table
(Int) ply(X1,1) ply(X2,1) ply(X1,2) ply(X1,3) ply(X2,2) ply(X2,3)
1 95.42 29.92 35.70
4 95.42 28.65 +
2 95.42 36.48 +
5 95.42 + +
7 95.42 28.72 +
3 95.42 36.81 +
6 95.42 + +
8 95.42 + +
9 95.42 + +
family d1 d2 df logLik AICc delta weight
1 gaussian(identity) 1 1 4 28.156 69.3 0.00 0.539
4 gaussian(identity) 1 2 5 25.783 70.1 0.83 0.357
2 gaussian(identity) 2 1 5 27.303 73.2 3.86 0.078
5 gaussian(identity) 2 2 6 25.354 76.7 7.40 0.013
7 gaussian(identity) 1 3 6 25.769 77.5 8.23 0.009
3 gaussian(identity) 3 1 6 26.541 79.1 9.77 0.004
6 gaussian(identity) 3 2 7 24.811 86.0 16.71 0.000
8 gaussian(identity) 2 3 7 25.246 86.9 17.58 0.000
9 gaussian(identity) 3 3 8 24.487 101.0 31.66 0.000
Models ranked by AICc(x)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.