Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
in R Programming by (50.2k points)

I just need a function that operates a custom model with me with one free variable.

model <- function(x){

  lmer(paste("cyl ~", x, "+ (1|disp)"), data = .)


And after that, I want to use this in dplyr's do():

mtcars %>% 

  do(x = model("hp"))

How should I approach this problem?

1 Answer

0 votes
by (108k points)

For that you first have to pass the data to the function :



model <- function(data, x){

  lmer(paste("cyl ~", x, "+", "(1|disp)"), data = data)


and then try to call it like the following :

mtcars %>% model('hp')

#Linear mixed model fit by REML ['lmerMod']

#Formula: cyl ~ hp + (1 | disp)

#   Data: data

#REML criterion at convergence: 96.2

#Random effects:

# Groups   Name        Std.Dev.

# disp     (Intercept) 0.927   

# Residual             0.441   

#Number of obs: 32, groups:  disp, 27

#Fixed Effects:

#(Intercept)           hp  

#     3.1866       0.0196  

If you are interested in certification of R the do check out the R programming certification

Browse Categories