ParseFormula.Rd
Create a model frame for a formula object, by handling the left hand side the same way the right hand side is handled in model.frame. Especially variables separated by + are interpreted as separate variables.
ParseFormula(formula, data = parent.frame(), drop = TRUE)
an object of class "formula" (or one that can be coerced to that class): a symbolic description for the variables to be described.
an optional data frame, list or environment (or object coercible by as.data.frame to a data frame) containing the variables in the model. If not found in data, the variables are taken from environment(formula), typically the environment from which lm is called.
if drop
is TRUE
, unused factor levels are dropped from the result when creating interaction terms.
The default is to drop all unused factor levels.
This is used by Desc.formula
for describing data by groups while remaining flexible for using
I(...)
constructions, functions or interaction terms.
a list of 3 elements
the formula which had to be parsed
a list of 3 elements:
mf: data.frame, the model.frame of the left hand side of the formula
mf.eval: data.frame, the evaluated model.frame of the left hand side of the formula
vars: the names of the evaluated model.frame
a list of 3 elements:
mf: data.frame, the model.frame of the right hand side of the formula
mf.eval: data.frame, the evaluated model.frame of the right hand side of the formula
vars: the names of the evaluated model.frame
The functions used to handle formulas: model.frame
, terms
, formula
Used in: Desc.formula
set.seed(17)
piz <- d.pizza[sample(nrow(d.pizza),10), c("temperature","price","driver","weekday")]
f1 <- formula(. ~ driver)
f2 <- formula(temperature ~ .)
f3 <- formula(temperature + price ~ .)
f4 <- formula(temperature ~ . - driver)
f5 <- formula(temperature + price ~ driver)
f6 <- formula(temperature + price ~ driver * weekday)
f7 <- formula(I(temperature^2) + sqrt(price) ~ driver + weekday)
f8 <- formula(temperature + price ~ 1)
f9 <- formula(temperature + price ~ driver * weekday - price)
ParseFormula(f1, data=piz)
#> $formula
#> . ~ driver
#> <environment: 0x5617afce2640>
#>
#> $lhs
#> $lhs$mf
#> temperature price weekday
#> 108 61.5 62.955 1
#> 495 53.8 73.746 5
#> 423 53.8 21.980 3
#> 150 45.7 47.664 3
#> 1153 47.5 16.990 7
#> 721 30.7 36.970 4
#> 458 57.6 31.980 4
#> 774 38.6 78.237 5
#> 1182 33.8 45.864 1
#> 404 35.5 31.176 2
#>
#> $lhs$mf.eval
#> temperature price weekday
#> 108 61.5 62.955 1
#> 495 53.8 73.746 5
#> 423 53.8 21.980 3
#> 150 45.7 47.664 3
#> 1153 47.5 16.990 7
#> 721 30.7 36.970 4
#> 458 57.6 31.980 4
#> 774 38.6 78.237 5
#> 1182 33.8 45.864 1
#> 404 35.5 31.176 2
#>
#> $lhs$vars
#> [1] "temperature" "price" "weekday"
#>
#>
#> $rhs
#> $rhs$mf
#> driver
#> 108 Hunter
#> 495 Carpenter
#> 423 Carter
#> 150 Carter
#> 1153 Miller
#> 721 Carpenter
#> 458 Carter
#> 774 Miller
#> 1182 Carpenter
#> 404 Taylor
#>
#> $rhs$mf.eval
#> driver
#> 108 Hunter
#> 495 Carpenter
#> 423 Carter
#> 150 Carter
#> 1153 Miller
#> 721 Carpenter
#> 458 Carter
#> 774 Miller
#> 1182 Carpenter
#> 404 Taylor
#>
#> $rhs$vars
#> [1] "driver"
#>
#>
ParseFormula(f2, data=piz)
#> $formula
#> temperature ~ .
#> <environment: 0x5617afce2640>
#>
#> $lhs
#> $lhs$mf
#> temperature
#> 108 61.5
#> 495 53.8
#> 423 53.8
#> 150 45.7
#> 1153 47.5
#> 721 30.7
#> 458 57.6
#> 774 38.6
#> 1182 33.8
#> 404 35.5
#>
#> $lhs$mf.eval
#> temperature
#> 108 61.5
#> 495 53.8
#> 423 53.8
#> 150 45.7
#> 1153 47.5
#> 721 30.7
#> 458 57.6
#> 774 38.6
#> 1182 33.8
#> 404 35.5
#>
#> $lhs$vars
#> [1] "temperature"
#>
#>
#> $rhs
#> $rhs$mf
#> price driver weekday
#> 108 62.955 Hunter 1
#> 495 73.746 Carpenter 5
#> 423 21.980 Carter 3
#> 150 47.664 Carter 3
#> 1153 16.990 Miller 7
#> 721 36.970 Carpenter 4
#> 458 31.980 Carter 4
#> 774 78.237 Miller 5
#> 1182 45.864 Carpenter 1
#> 404 31.176 Taylor 2
#>
#> $rhs$mf.eval
#> price driver weekday
#> 108 62.955 Hunter 1
#> 495 73.746 Carpenter 5
#> 423 21.980 Carter 3
#> 150 47.664 Carter 3
#> 1153 16.990 Miller 7
#> 721 36.970 Carpenter 4
#> 458 31.980 Carter 4
#> 774 78.237 Miller 5
#> 1182 45.864 Carpenter 1
#> 404 31.176 Taylor 2
#>
#> $rhs$vars
#> [1] "price" "driver" "weekday"
#>
#>
ParseFormula(f3, data=piz)
#> $formula
#> temperature + price ~ .
#> <environment: 0x5617afce2640>
#>
#> $lhs
#> $lhs$mf
#> temperature price
#> 108 61.5 62.955
#> 495 53.8 73.746
#> 423 53.8 21.980
#> 150 45.7 47.664
#> 1153 47.5 16.990
#> 721 30.7 36.970
#> 458 57.6 31.980
#> 774 38.6 78.237
#> 1182 33.8 45.864
#> 404 35.5 31.176
#>
#> $lhs$mf.eval
#> temperature price
#> 108 61.5 62.955
#> 495 53.8 73.746
#> 423 53.8 21.980
#> 150 45.7 47.664
#> 1153 47.5 16.990
#> 721 30.7 36.970
#> 458 57.6 31.980
#> 774 38.6 78.237
#> 1182 33.8 45.864
#> 404 35.5 31.176
#>
#> $lhs$vars
#> [1] "temperature" "price"
#>
#>
#> $rhs
#> $rhs$mf
#> driver weekday
#> 108 Hunter 1
#> 495 Carpenter 5
#> 423 Carter 3
#> 150 Carter 3
#> 1153 Miller 7
#> 721 Carpenter 4
#> 458 Carter 4
#> 774 Miller 5
#> 1182 Carpenter 1
#> 404 Taylor 2
#>
#> $rhs$mf.eval
#> driver weekday
#> 108 Hunter 1
#> 495 Carpenter 5
#> 423 Carter 3
#> 150 Carter 3
#> 1153 Miller 7
#> 721 Carpenter 4
#> 458 Carter 4
#> 774 Miller 5
#> 1182 Carpenter 1
#> 404 Taylor 2
#>
#> $rhs$vars
#> [1] "driver" "weekday"
#>
#>
ParseFormula(f4, data=piz)
#> $formula
#> temperature ~ . - driver
#> <environment: 0x5617afce2640>
#>
#> $lhs
#> $lhs$mf
#> temperature
#> 108 61.5
#> 495 53.8
#> 423 53.8
#> 150 45.7
#> 1153 47.5
#> 721 30.7
#> 458 57.6
#> 774 38.6
#> 1182 33.8
#> 404 35.5
#>
#> $lhs$mf.eval
#> temperature
#> 108 61.5
#> 495 53.8
#> 423 53.8
#> 150 45.7
#> 1153 47.5
#> 721 30.7
#> 458 57.6
#> 774 38.6
#> 1182 33.8
#> 404 35.5
#>
#> $lhs$vars
#> [1] "temperature"
#>
#>
#> $rhs
#> $rhs$mf
#> price weekday
#> 108 62.955 1
#> 495 73.746 5
#> 423 21.980 3
#> 150 47.664 3
#> 1153 16.990 7
#> 721 36.970 4
#> 458 31.980 4
#> 774 78.237 5
#> 1182 45.864 1
#> 404 31.176 2
#>
#> $rhs$mf.eval
#> price weekday
#> 108 62.955 1
#> 495 73.746 5
#> 423 21.980 3
#> 150 47.664 3
#> 1153 16.990 7
#> 721 36.970 4
#> 458 31.980 4
#> 774 78.237 5
#> 1182 45.864 1
#> 404 31.176 2
#>
#> $rhs$vars
#> [1] "price" "weekday"
#>
#>
ParseFormula(f5, data=piz)
#> $formula
#> temperature + price ~ driver
#> <environment: 0x5617afce2640>
#>
#> $lhs
#> $lhs$mf
#> temperature price
#> 108 61.5 62.955
#> 495 53.8 73.746
#> 423 53.8 21.980
#> 150 45.7 47.664
#> 1153 47.5 16.990
#> 721 30.7 36.970
#> 458 57.6 31.980
#> 774 38.6 78.237
#> 1182 33.8 45.864
#> 404 35.5 31.176
#>
#> $lhs$mf.eval
#> temperature price
#> 108 61.5 62.955
#> 495 53.8 73.746
#> 423 53.8 21.980
#> 150 45.7 47.664
#> 1153 47.5 16.990
#> 721 30.7 36.970
#> 458 57.6 31.980
#> 774 38.6 78.237
#> 1182 33.8 45.864
#> 404 35.5 31.176
#>
#> $lhs$vars
#> [1] "temperature" "price"
#>
#>
#> $rhs
#> $rhs$mf
#> driver
#> 108 Hunter
#> 495 Carpenter
#> 423 Carter
#> 150 Carter
#> 1153 Miller
#> 721 Carpenter
#> 458 Carter
#> 774 Miller
#> 1182 Carpenter
#> 404 Taylor
#>
#> $rhs$mf.eval
#> driver
#> 108 Hunter
#> 495 Carpenter
#> 423 Carter
#> 150 Carter
#> 1153 Miller
#> 721 Carpenter
#> 458 Carter
#> 774 Miller
#> 1182 Carpenter
#> 404 Taylor
#>
#> $rhs$vars
#> [1] "driver"
#>
#>
ParseFormula(f6, data=piz)
#> $formula
#> temperature + price ~ driver * weekday
#> <environment: 0x5617afce2640>
#>
#> $lhs
#> $lhs$mf
#> temperature price
#> 108 61.5 62.955
#> 495 53.8 73.746
#> 423 53.8 21.980
#> 150 45.7 47.664
#> 1153 47.5 16.990
#> 721 30.7 36.970
#> 458 57.6 31.980
#> 774 38.6 78.237
#> 1182 33.8 45.864
#> 404 35.5 31.176
#>
#> $lhs$mf.eval
#> temperature price
#> 108 61.5 62.955
#> 495 53.8 73.746
#> 423 53.8 21.980
#> 150 45.7 47.664
#> 1153 47.5 16.990
#> 721 30.7 36.970
#> 458 57.6 31.980
#> 774 38.6 78.237
#> 1182 33.8 45.864
#> 404 35.5 31.176
#>
#> $lhs$vars
#> [1] "temperature" "price"
#>
#>
#> $rhs
#> $rhs$mf
#> driver weekday
#> 108 Hunter 1
#> 495 Carpenter 5
#> 423 Carter 3
#> 150 Carter 3
#> 1153 Miller 7
#> 721 Carpenter 4
#> 458 Carter 4
#> 774 Miller 5
#> 1182 Carpenter 1
#> 404 Taylor 2
#>
#> $rhs$mf.eval
#> driver weekday driver:weekday
#> 108 Hunter 1 Hunter:1
#> 495 Carpenter 5 Carpenter:5
#> 423 Carter 3 Carter:3
#> 150 Carter 3 Carter:3
#> 1153 Miller 7 Miller:7
#> 721 Carpenter 4 Carpenter:4
#> 458 Carter 4 Carter:4
#> 774 Miller 5 Miller:5
#> 1182 Carpenter 1 Carpenter:1
#> 404 Taylor 2 Taylor:2
#>
#> $rhs$vars
#> [1] "driver" "weekday" "driver:weekday"
#>
#>
ParseFormula(f7, data=piz)
#> $formula
#> I(temperature^2) + sqrt(price) ~ driver + weekday
#> <environment: 0x5617afce2640>
#>
#> $lhs
#> $lhs$mf
#> I(temperature^2) sqrt(price)
#> 108 3782.25 7.934419
#> 495 2894.44 8.587549
#> 423 2894.44 4.688283
#> 150 2088.49 6.903912
#> 1153 2256.25 4.121893
#> 721 942.49 6.080296
#> 458 3317.76 5.655086
#> 774 1489.96 8.845168
#> 1182 1142.44 6.772297
#> 404 1260.25 5.583547
#>
#> $lhs$mf.eval
#> I(temperature^2) sqrt(price)
#> 108 3782.25 7.934419
#> 495 2894.44 8.587549
#> 423 2894.44 4.688283
#> 150 2088.49 6.903912
#> 1153 2256.25 4.121893
#> 721 942.49 6.080296
#> 458 3317.76 5.655086
#> 774 1489.96 8.845168
#> 1182 1142.44 6.772297
#> 404 1260.25 5.583547
#>
#> $lhs$vars
#> [1] "I(temperature^2)" "sqrt(price)"
#>
#>
#> $rhs
#> $rhs$mf
#> driver weekday
#> 108 Hunter 1
#> 495 Carpenter 5
#> 423 Carter 3
#> 150 Carter 3
#> 1153 Miller 7
#> 721 Carpenter 4
#> 458 Carter 4
#> 774 Miller 5
#> 1182 Carpenter 1
#> 404 Taylor 2
#>
#> $rhs$mf.eval
#> driver weekday
#> 108 Hunter 1
#> 495 Carpenter 5
#> 423 Carter 3
#> 150 Carter 3
#> 1153 Miller 7
#> 721 Carpenter 4
#> 458 Carter 4
#> 774 Miller 5
#> 1182 Carpenter 1
#> 404 Taylor 2
#>
#> $rhs$vars
#> [1] "driver" "weekday"
#>
#>
ParseFormula(f8, data=piz)
#> $formula
#> temperature + price ~ 1
#> <environment: 0x5617afce2640>
#>
#> $lhs
#> $lhs$mf
#> temperature price
#> 108 61.5 62.955
#> 495 53.8 73.746
#> 423 53.8 21.980
#> 150 45.7 47.664
#> 1153 47.5 16.990
#> 721 30.7 36.970
#> 458 57.6 31.980
#> 774 38.6 78.237
#> 1182 33.8 45.864
#> 404 35.5 31.176
#>
#> $lhs$mf.eval
#> temperature price
#> 108 61.5 62.955
#> 495 53.8 73.746
#> 423 53.8 21.980
#> 150 45.7 47.664
#> 1153 47.5 16.990
#> 721 30.7 36.970
#> 458 57.6 31.980
#> 774 38.6 78.237
#> 1182 33.8 45.864
#> 404 35.5 31.176
#>
#> $lhs$vars
#> [1] "temperature" "price"
#>
#>
#> $rhs
#> $rhs$mf
#> data frame with 0 columns and 10 rows
#>
#> $rhs$mf.eval
#> data frame with 0 columns and 10 rows
#>
#> $rhs$vars
#> character(0)
#>
#>