I am writing a function that takes two variables and separately regresses each of them on a set of controls expressed as a one-sided formula. Right now I'm using the following to make the formula for one of the regressions, but it feels a bit hacked-up:
foo <- function(x, y, controls) {
cl <- match.call()
xn <- cl[["x"]]
xf <- as.formula(paste(xn, deparse(controls)))
}
I'd prefer to do this using update.formula()
, but of course update.formula(controls, x ~ .)
and update.formula(controls, as.name(x) ~ .)
don't work. What should I be doing?
You can create a formula to calculate values in your worksheet by using a function. For example, the formulas =SUM(A1:A2) and SUM(A1,A2) both use the SUM function to add the values in cells A1 and A2.
1) Using the Insert Function button to display the Function Arguments dialog box. 2) Using the shortcut key (Shift + F3) to display the Function Arguments dialog box. 3) Editing the formulas manually using the Formula Bar. 4) Editing the formulas manually using the shortcut key (F2).
Argument Vs. Parameter : Explore the Major Difference between Argument and Parameter. The values that are declared within a function when the function is called are known as an argument. Whereas, the variables that are defined when the function is declared are known as a parameter.
Here's one approach:
right <- ~ a + b + c
left <- ~ y
left_2 <- substitute(left ~ ., list(left = left[[2]]))
update(right, left_2)
But I think you'll have to either paste text strings together, or use substitute. To the best of my knowledge, there are no functions to create one two sided formula from two one-sided formulas (or similar equivalents).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With