R - could not find function "cld"

Viewed 3193

I'm trying to apply these codes for https://rcompanion.org/rcompanion/d_08.html

my problem

library(multcompView)
> library(lsmeans)
> lsmeans = lsmeans::lsmeans ### Uses the lsmeans function
> leastsquare = lsmeans(model,
+                       "B_exp_type",
+                       adjust="tukey")
NOTE: Results may be misleading due to involvement in interactions

   > cld(leastsquare,
    +     alpha=.05, 
    +     Letters=letters)
    Error in cld(leastsquare, alpha = 0.05, Letters = letters) : 
      could not find function "cld"

how to fix this could not find function "cld" error ??

3 Answers

You need to install the multcomp package.

Best advert for namespacing I've seen for a while.

I am the author of that page.

There are many minor updates I need to do to that site. One is updating all calls to the lsmeans package to the emmeans package. Unfortunately, I used lsmeans like 100 times, so it's a lot of little updates.

As mentioned, you can call cld from multcomp. The following page lists options for that call regarding an emmeans object:

rdrr.io/cran/emmeans/man/CLD.emmGrid.html

The following code gives an example:

library(emmeans)

warp.lm = lm(breaks ~ wool * tension, data = warpbreaks)

marginal = emmeans(warp.lm, ~ tension:wool)

pwpp(marginal)

library(multcomp)

cld(marginal, reversed=FALSE, alpha=0.05, details=FALSE, 
    adjust="sidak", level=0.95, Letters=letters)

Adding to @Keith McNulty's answer: Besides installing and loading the {multcomp} package, you must also have the {multcompView} package installed to use multcomp::cld().

Here is a reprex of when {multcompView} is not installed:

library(emmeans)
library(multcomp)
library(multcompView)
#> Error in library(multcompView): there is no package called 'multcompView'

mod <- lm(weight ~ group, data = PlantGrowth)
emm <- emmeans(mod, "group")
cld(emm, Letters = letters)
#> Error in .requireNS("multcompView", "The 'multcompView' package must be installed to use CLD methods"): The 'multcompView' package must be installed to use CLD methods

And here is one where it is installed:

library(emmeans)
library(multcomp)
library(multcompView)

mod <- lm(weight ~ group, data = PlantGrowth)
emm <- emmeans(mod, "group")
cld(emm, Letters = letters)
#>  group emmean    SE df lower.CL upper.CL .group
#>  trt1    4.66 0.197 27     4.26     5.07  a    
#>  ctrl    5.03 0.197 27     4.63     5.44  ab   
#>  trt2    5.53 0.197 27     5.12     5.93   b   

Also, here is a short chapter I wrote on the compact letter display.

Related