Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Easy computation of Marketing Metrics in R

License

NotificationsYou must be signed in to change notification settings

y-bar/mmetrics

Repository files navigation

Travis-CI Build StatusBuild statusCRAN_Status_BadgecodecovLicenceLifecycle: experimental

Easy Computation of Marketing Metrics with Different Analysis Axis.

Installation

You can install the released version of {mmetrics} from CRAN with:

install.packages("mmetrics")

Or install the development version from github with:

# install.packages("remotes")remotes::install_github("y-bar/mmetrics")

Example

Load Dummy data

First, we load dummy data from {mmetrics} package for this example.

df<-mmetrics::dummy_datadf#>    gender age cost impression click conversion#> 1       M  10   51        101     0          0#> 2       F  20   52        102     3          1#> 3       M  30   53        103     6          2#> 4       F  40   54        104     9          3#> 5       M  50   55        105    12          4#> 6       F  60   56        106    15          5#> 7       M  70   57        107    18          6#> 8       F  80   58        108    21          7#> 9       M  90   59        109    24          8#> 10      F 100   60        110    27          9

Define metrics

As a next step, we define metrics to evaluate usingmmetrics::define.

# Example metricsmetrics<-mmetrics::define(cost= sum(cost),ctr= sum(click)/sum(impression)# CTR, Click Through Rate)

Just callmmetrics::add() !

Callmmetrics::add() with grouping key (heregender) then we willget newdata.frame with defined metrics.

mmetrics::add(df,gender,metrics=metrics)#> # A tibble: 2 x 3#>   gender  cost   ctr#>   <fct>  <int> <dbl>#> 1 F        280 0.142#> 2 M        275 0.114

Remove aggregate function from metrics usingmmetrics::disaggregate()

It is hassle for users to re-define metrics when you would like to usethese fordplyr::mutate(). In this case, you can usemmetrics::disaggregate() to removethe first aggregation functionfor the argument and return disaggregated metrics.

# Original metrics. sum() is used for this metricsmetrics#> <list_of<quosure>>#>#> $cost#> <quosure>#> expr: ^sum(cost)#> env:  global#>#> $ctr#> <quosure>#> expr: ^sum(click) / sum(impression)#> env:  global
# Disaggregate metrics!metrics_disaggregated<-mmetrics::disaggregate(metrics)# Woo! sum() are removed!!!metrics_disaggregated#> $cost#> <quosure>#> expr: ^cost#> env:  global#>#> $ctr#> <quosure>#> expr: ^click / impression#> env:  global

You can use these metrics withdplyr::mutate() for row-wise metricscomputation.

dplyr::mutate(df,!!!metrics_disaggregated)#>    gender age cost impression click conversion        ctr#> 1       M  10   51        101     0          0 0.00000000#> 2       F  20   52        102     3          1 0.02941176#> 3       M  30   53        103     6          2 0.05825243#> 4       F  40   54        104     9          3 0.08653846#> 5       M  50   55        105    12          4 0.11428571#> 6       F  60   56        106    15          5 0.14150943#> 7       M  70   57        107    18          6 0.16822430#> 8       F  80   58        108    21          7 0.19444444#> 9       M  90   59        109    24          8 0.22018349#> 10      F 100   60        110    27          9 0.24545455

…or, you can do the same compucation usingmmetrics::gmutate() defindin our package. In this case, you do not need to write!!!(bang-bang-bang) operator explicitly.

mmetrics::gmutate(df,metrics=metrics_disaggregated)#> # A tibble: 10 x 7#>    gender   age  cost impression click conversion    ctr#>    <fct>  <dbl> <int>      <int> <dbl>      <int>  <dbl>#>  1 M         10    51        101     0          0 0#>  2 F         20    52        102     3          1 0.0294#>  3 M         30    53        103     6          2 0.0583#>  4 F         40    54        104     9          3 0.0865#>  5 M         50    55        105    12          4 0.114#>  6 F         60    56        106    15          5 0.142#>  7 M         70    57        107    18          6 0.168#>  8 F         80    58        108    21          7 0.194#>  9 M         90    59        109    24          8 0.220#> 10 F        100    60        110    27          9 0.245

More examples


[8]ページ先頭

©2009-2025 Movatter.jp