1313# ' get_emtrends(model)
1414# ' get_emtrends(model, by = "Sepal.Width")
1515# ' @export
16- get_emtrends <- function (model ,
17- trend = NULL ,
18- by = NULL ,
19- predict = NULL ,
20- keep_iterations = FALSE ,
21- verbose = TRUE ,
22- ... ) {
16+ get_emtrends <- function (
17+ model ,
18+ trend = NULL ,
19+ by = NULL ,
20+ predict = NULL ,
21+ keep_iterations = FALSE ,
22+ verbose = TRUE ,
23+ ...
24+ ) {
2325 # check if available
2426 insight :: check_if_installed(" emmeans" )
2527
@@ -38,7 +40,11 @@ get_emtrends <- function(model,
3840 ))
3941
4042 # handle distributional parameters
41- if (! is.null(predict ) && inherits(model , " brmsfit" ) && predict %in% .brms_aux_elements(model )) {
43+ if (
44+ ! is.null(predict ) &&
45+ inherits(model , " brmsfit" ) &&
46+ predict %in% .brms_aux_elements(model )
47+ ) {
4248 fun_args $ dpar <- predict
4349 } else {
4450 fun_args $ type <- predict
@@ -70,11 +76,13 @@ get_emtrends <- function(model,
7076# =========================================================================
7177
7278# ' @keywords internal
73- .guess_emtrends_arguments <- function (model ,
74- trend = NULL ,
75- by = NULL ,
76- verbose = TRUE ,
77- ... ) {
79+ .guess_emtrends_arguments <- function (
80+ model ,
81+ trend = NULL ,
82+ by = NULL ,
83+ verbose = TRUE ,
84+ ...
85+ ) {
7886 # Gather info
7987 model_data <- insight :: get_data(model , verbose = FALSE )
8088 predictors <- intersect(
@@ -86,19 +94,37 @@ get_emtrends <- function(model,
8694 if (is.null(trend )) {
8795 trend <- predictors [sapply(model_data [predictors ], is.numeric )][1 ]
8896 if (! length(trend ) || is.na(trend )) {
89- insight :: format_error(" Model contains no numeric predictor. Please specify `trend`." )
97+ insight :: format_error(
98+ " Model contains no numeric predictor. Please specify `trend`."
99+ )
90100 }
91101 if (verbose ) {
92- insight :: format_alert(paste0(" No numeric variable was specified for slope estimation. Selecting `trend = \" " , trend , " \" `." )) # nolint
102+ insight :: format_alert(paste0(
103+ " No numeric variable was specified for slope estimation. Selecting `trend = \" " ,
104+ trend ,
105+ " \" `."
106+ ))
93107 }
94108 }
95109 if (length(trend ) > 1 ) {
96110 trend <- trend [1 ]
97111 if (verbose ) {
98- insight :: format_alert(paste0(" More than one numeric variable was selected for slope estimation. Keeping only " , trend [1 ], " ." )) # nolint
112+ insight :: format_alert(paste0(
113+ " More than one numeric variable was selected for slope estimation. Keeping only `" ,
114+ trend [1 ],
115+ " `."
116+ ))
99117 }
100118 }
101119
120+ if (trend %in% colnames(model_data ) && ! is.numeric(model_data [[trend ]])) {
121+ insight :: format_error(paste0(
122+ " Variable `" ,
123+ trend ,
124+ " ` is not numeric. Slopes can only be estimated for numeric variables when `backend = \" emmeans\" `. Please use `estimate_contrasts()` instead."
125+ ))
126+ }
127+
102128 my_args <- list (trend = trend , by = by )
103129 .process_emmeans_arguments(model , args = my_args , data = model_data , ... )
104130}
0 commit comments