BioEvo
BioEvo

Reputation: 1

How to fix error in step function (stepcACI): rep(no, length.out = len)

I got the following error running the stepcAIC function in a linear mixed effect model (lmer):

Fehler in rep(no, length.out = len) : attempt to replicate an object of type 'language'

I don't understand what the error is saying. All the factors are declared as "factors" and all the other variables as "numeric". The storage.mode is "integer" for the factors and "double" for the other variables.

This is my model and the step function:

biom_FULLO<-lmer((above_bio)~MUM_germ_time+MUM_num_seed+MUM_av_seed_mass+
          MUM_num_inf+MUM_above_bio+MUM_total_bio+MUM_inf_size+MUM_root_bio+
          MUM_CV_seed_mass+MUM_CV_SEM_per_inflor+MUM_CV_inflor_size+
          MUM_seed_weight+germ_date_year+germ_time+flow_start_date+
          height_3m+height_flow+num_inf+seed_gen+Early+
          Late+seed_gen:Early+seed_gen:Late+
          Early:Late+seed_gen:Early:Late+(1|ID_year), DT_gen_biom)

biom_step<-stepcAIC(biom_FULLO, direction = "backward", 
      trace = FALSE, data = DT_gen_biom)

Any idea anyone?

P.D.: traceback()

6: ifelse(wasGam, formula(modelInit$gam)[[2]], formula(modelInit)[[2]])
5: makeFormula(x, modelInit)
4: FUN(X[[i]], ...)
3: lapply(newSetup, function(x) makeFormula(x, modelInit))
2: calculateAllCAICs(newSetup = newSetup, modelInit = object, numCores = numCores, 
       data = data, calcNonOptimMod = calcNonOptimMod, nrmods = numberOfSavedModels, 
       ...)
1: stepcAIC(biom_FULLO, direction = "backward", trace = FALSE, data = DT_gen_biom)

Upvotes: 0

Views: 153

Answers (1)

danlooo
danlooo

Reputation: 10637

Just remove the parenthesis of your response variable in the LHS of your formula:

library(cAIC4)
#> Loading required package: lme4
#> Loading required package: Matrix
#> Loading required package: stats4
#> Loading required package: nlme
#> 
#> Attaching package: 'nlme'
#> The following object is masked from 'package:lme4':
#> 
#>     lmList
library(lme4)

m1 <- lmer((Sepal.Length) ~ Sepal.Width + (1|Species), data = iris)
stepcAIC(m1)
#> Warning in nobars(formula(modelInit)) == formula(modelInit)[[2]]: longer object
#> length is not a multiple of shorter object length
#> Error in rep(no, length.out = len): attempt to replicate an object of type 'language'

m2 <- lmer(Sepal.Length ~ Sepal.Width + (1|Species), data = iris)
stepcAIC(m2)
#> $finalModel
#> Linear mixed model fit by REML ['lmerMod']
#> Formula: Sepal.Length ~ Sepal.Width + (1 | Species)
#>    Data: iris
#> REML criterion at convergence: 194.6361
#> Random effects:
#>  Groups   Name        Std.Dev.
#>  Species  (Intercept) 1.010   
#>  Residual             0.438   
#> Number of obs: 150, groups:  Species, 3
#> Fixed Effects:
#> (Intercept)  Sepal.Width  
#>      3.4062       0.7972  
#> 
#> $additionalModels
#> NULL
#> 
#> $bestCAIC
#> [1] 184.0044

Created on 2022-02-11 by the reprex package (v2.0.0)

Upvotes: 1

Related Questions