ParseFormula: Parse a Formula and Create a Model Frame

Description Usage Arguments Details Value Author(s) See Also Examples

View source: R/DescTools.r

Description

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.

Usage

1

Arguments

formula

an object of class "formula" (or one that can be coerced to that class): a symbolic description for the variables to be described.

data

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.

drop

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.

Details

This is used by Desc.formula for describing data by groups while remaining flexible for using I(...) constructions, functions or interaction terms.

Value

a list of 3 elements

formula

the formula which had to be parsed

lhs

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

rhs

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

Author(s)

Andri Signorell <andri@signorell.net>

See Also

The functions used to handle formulas: model.frame, terms, formula
Used in: Desc.formula

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
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)  
ParseFormula(f2, data=piz)  
ParseFormula(f3, data=piz)
ParseFormula(f4, data=piz)
ParseFormula(f5, data=piz)
ParseFormula(f6, data=piz)
ParseFormula(f7, data=piz)
ParseFormula(f8, data=piz)

Example output

$formula
. ~ driver

$lhs
$lhs$mf
     temperature   price weekday
188         44.2  56.655       4
1170        47.3  97.407       7
566         47.5  25.980       6
937         55.9  51.084       2
492         58.8  53.064       5
649         61.9  47.664       1
249         38.8 110.007       5
225         45.7  77.346       5
1206        48.3  42.970       1
233         47.1  58.455       5

$lhs$mf.eval
     temperature   price weekday
188         44.2  56.655       4
1170        47.3  97.407       7
566         47.5  25.980       6
937         55.9  51.084       2
492         58.8  53.064       5
649         61.9  47.664       1
249         38.8 110.007       5
225         45.7  77.346       5
1206        48.3  42.970       1
233         47.1  58.455       5

$lhs$vars
[1] "temperature" "price"       "weekday"    


$rhs
$rhs$mf
        driver
188     Carter
1170 Carpenter
566     Miller
937     Farmer
492     Farmer
649     Hunter
249  Carpenter
225     Miller
1206    Carter
233     Carter

$rhs$mf.eval
        driver
188     Carter
1170 Carpenter
566     Miller
937     Farmer
492     Farmer
649     Hunter
249  Carpenter
225     Miller
1206    Carter
233     Carter

$rhs$vars
[1] "driver"


$formula
temperature ~ .

$lhs
$lhs$mf
     temperature
188         44.2
1170        47.3
566         47.5
937         55.9
492         58.8
649         61.9
249         38.8
225         45.7
1206        48.3
233         47.1

$lhs$mf.eval
     temperature
188         44.2
1170        47.3
566         47.5
937         55.9
492         58.8
649         61.9
249         38.8
225         45.7
1206        48.3
233         47.1

$lhs$vars
[1] "temperature"


$rhs
$rhs$mf
       price    driver weekday
188   56.655    Carter       4
1170  97.407 Carpenter       7
566   25.980    Miller       6
937   51.084    Farmer       2
492   53.064    Farmer       5
649   47.664    Hunter       1
249  110.007 Carpenter       5
225   77.346    Miller       5
1206  42.970    Carter       1
233   58.455    Carter       5

$rhs$mf.eval
       price    driver weekday
188   56.655    Carter       4
1170  97.407 Carpenter       7
566   25.980    Miller       6
937   51.084    Farmer       2
492   53.064    Farmer       5
649   47.664    Hunter       1
249  110.007 Carpenter       5
225   77.346    Miller       5
1206  42.970    Carter       1
233   58.455    Carter       5

$rhs$vars
[1] "price"   "driver"  "weekday"


$formula
temperature + price ~ .

$lhs
$lhs$mf
     temperature   price
188         44.2  56.655
1170        47.3  97.407
566         47.5  25.980
937         55.9  51.084
492         58.8  53.064
649         61.9  47.664
249         38.8 110.007
225         45.7  77.346
1206        48.3  42.970
233         47.1  58.455

$lhs$mf.eval
     temperature   price
188         44.2  56.655
1170        47.3  97.407
566         47.5  25.980
937         55.9  51.084
492         58.8  53.064
649         61.9  47.664
249         38.8 110.007
225         45.7  77.346
1206        48.3  42.970
233         47.1  58.455

$lhs$vars
[1] "temperature" "price"      


$rhs
$rhs$mf
        driver weekday
188     Carter       4
1170 Carpenter       7
566     Miller       6
937     Farmer       2
492     Farmer       5
649     Hunter       1
249  Carpenter       5
225     Miller       5
1206    Carter       1
233     Carter       5

$rhs$mf.eval
        driver weekday
188     Carter       4
1170 Carpenter       7
566     Miller       6
937     Farmer       2
492     Farmer       5
649     Hunter       1
249  Carpenter       5
225     Miller       5
1206    Carter       1
233     Carter       5

$rhs$vars
[1] "driver"  "weekday"


$formula
temperature ~ . - driver

$lhs
$lhs$mf
     temperature
188         44.2
1170        47.3
566         47.5
937         55.9
492         58.8
649         61.9
249         38.8
225         45.7
1206        48.3
233         47.1

$lhs$mf.eval
     temperature
188         44.2
1170        47.3
566         47.5
937         55.9
492         58.8
649         61.9
249         38.8
225         45.7
1206        48.3
233         47.1

$lhs$vars
[1] "temperature"


$rhs
$rhs$mf
       price weekday
188   56.655       4
1170  97.407       7
566   25.980       6
937   51.084       2
492   53.064       5
649   47.664       1
249  110.007       5
225   77.346       5
1206  42.970       1
233   58.455       5

$rhs$mf.eval
       price weekday
188   56.655       4
1170  97.407       7
566   25.980       6
937   51.084       2
492   53.064       5
649   47.664       1
249  110.007       5
225   77.346       5
1206  42.970       1
233   58.455       5

$rhs$vars
[1] "price"   "weekday"


$formula
temperature + price ~ driver

$lhs
$lhs$mf
     temperature   price
188         44.2  56.655
1170        47.3  97.407
566         47.5  25.980
937         55.9  51.084
492         58.8  53.064
649         61.9  47.664
249         38.8 110.007
225         45.7  77.346
1206        48.3  42.970
233         47.1  58.455

$lhs$mf.eval
     temperature   price
188         44.2  56.655
1170        47.3  97.407
566         47.5  25.980
937         55.9  51.084
492         58.8  53.064
649         61.9  47.664
249         38.8 110.007
225         45.7  77.346
1206        48.3  42.970
233         47.1  58.455

$lhs$vars
[1] "temperature" "price"      


$rhs
$rhs$mf
        driver
188     Carter
1170 Carpenter
566     Miller
937     Farmer
492     Farmer
649     Hunter
249  Carpenter
225     Miller
1206    Carter
233     Carter

$rhs$mf.eval
        driver
188     Carter
1170 Carpenter
566     Miller
937     Farmer
492     Farmer
649     Hunter
249  Carpenter
225     Miller
1206    Carter
233     Carter

$rhs$vars
[1] "driver"


$formula
temperature + price ~ driver * weekday

$lhs
$lhs$mf
     temperature   price
188         44.2  56.655
1170        47.3  97.407
566         47.5  25.980
937         55.9  51.084
492         58.8  53.064
649         61.9  47.664
249         38.8 110.007
225         45.7  77.346
1206        48.3  42.970
233         47.1  58.455

$lhs$mf.eval
     temperature   price
188         44.2  56.655
1170        47.3  97.407
566         47.5  25.980
937         55.9  51.084
492         58.8  53.064
649         61.9  47.664
249         38.8 110.007
225         45.7  77.346
1206        48.3  42.970
233         47.1  58.455

$lhs$vars
[1] "temperature" "price"      


$rhs
$rhs$mf
        driver weekday
188     Carter       4
1170 Carpenter       7
566     Miller       6
937     Farmer       2
492     Farmer       5
649     Hunter       1
249  Carpenter       5
225     Miller       5
1206    Carter       1
233     Carter       5

$rhs$mf.eval
        driver weekday driver:weekday
188     Carter       4       Carter:4
1170 Carpenter       7    Carpenter:7
566     Miller       6       Miller:6
937     Farmer       2       Farmer:2
492     Farmer       5       Farmer:5
649     Hunter       1       Hunter:1
249  Carpenter       5    Carpenter:5
225     Miller       5       Miller:5
1206    Carter       1       Carter:1
233     Carter       5       Carter:5

$rhs$vars
[1] "driver"         "weekday"        "driver:weekday"


$formula
I(temperature^2) + sqrt(price) ~ driver + weekday

$lhs
$lhs$mf
     I(temperature^2) sqrt(price)
188           1953.64    7.526952
1170          2237.29    9.869498
566           2256.25    5.097058
937           3124.81    7.147307
492           3457.44    7.284504
649           3831.61    6.903912
249           1505.44   10.488422
225           2088.49    8.794657
1206          2332.89    6.555151
233           2218.41    7.645587

$lhs$mf.eval
     I(temperature^2) sqrt(price)
188           1953.64    7.526952
1170          2237.29    9.869498
566           2256.25    5.097058
937           3124.81    7.147307
492           3457.44    7.284504
649           3831.61    6.903912
249           1505.44   10.488422
225           2088.49    8.794657
1206          2332.89    6.555151
233           2218.41    7.645587

$lhs$vars
[1] "I(temperature^2)" "sqrt(price)"     


$rhs
$rhs$mf
        driver weekday
188     Carter       4
1170 Carpenter       7
566     Miller       6
937     Farmer       2
492     Farmer       5
649     Hunter       1
249  Carpenter       5
225     Miller       5
1206    Carter       1
233     Carter       5

$rhs$mf.eval
        driver weekday
188     Carter       4
1170 Carpenter       7
566     Miller       6
937     Farmer       2
492     Farmer       5
649     Hunter       1
249  Carpenter       5
225     Miller       5
1206    Carter       1
233     Carter       5

$rhs$vars
[1] "driver"  "weekday"


$formula
temperature + price ~ 1

$lhs
$lhs$mf
     temperature   price
188         44.2  56.655
1170        47.3  97.407
566         47.5  25.980
937         55.9  51.084
492         58.8  53.064
649         61.9  47.664
249         38.8 110.007
225         45.7  77.346
1206        48.3  42.970
233         47.1  58.455

$lhs$mf.eval
     temperature   price
188         44.2  56.655
1170        47.3  97.407
566         47.5  25.980
937         55.9  51.084
492         58.8  53.064
649         61.9  47.664
249         38.8 110.007
225         45.7  77.346
1206        48.3  42.970
233         47.1  58.455

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

DescTools documentation built on June 17, 2021, 5:12 p.m.