Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6943583
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:10:31+00:00 2026-05-27T13:10:31+00:00

I want to run a multiple comparisons analysis for the different variables of a

  • 0

I want to run a multiple comparisons analysis for the different variables of a model. My idea is as follows:

library(multcomp)
set.seed(123)
x1 <- gl(4,10)
x2 <- gl(5,2,40)
y <- rnorm(40)

fm1 <- lm(y ~ x1 + x2)

for(var in c('x1', 'x2'))
{
mc1 <- glht(fm1, linfct=mcp(var='Tukey'))
print(summary(mc1))
}

When I run, I get the following error:

Error en mcp2matrix(model, linfct = linfct) : 
    Variable(s) ‘var’ have been specified in ‘linfct’ but cannot be found in ‘model’! 

That is, it is not possible to use a character to specify an attribute of the mcp function.
Anyone knows a solution?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T13:10:31+00:00Added an answer on May 27, 2026 at 1:10 pm

    (Update: Make sure to see Hadley’s answer for the better way of doing this, without resorting to string-pasting. My answer will still be useful for explaining why that is harder-than-usual in this case.)

    The peculiarities of mcp() require you to use the relatively brute force approach of pasting together the expression you’d like to evaluate and then passing it through eval(parse()).

    The tricky bit is that mcp() interprets its first argument in a nonstandard way. Within mcp(), x1 = 'Tukey' does not (as it normally would) mean “assign a value of 'Tukey' to the argument x1“. Instead, the whole thing is interpreted as a symbolic description of the intended contrasts. (In this, it is much like more familiar formula objects such as the y ~ x1 + x2 in your lm() call).

    for(var in c('x1', 'x2')) {
        # Construct a character string with the expression you'd type at the command 
        # line. For example : "mcp(x1 = 'Tukey')"
        exprString <- paste("mcp(", var, "='Tukey')")
        # eval(parse()) it to get an 'mcp' object.
        LINFCT <- eval(parse(text = exprString))
        mc1 <- glht(fm1, linfct = LINFCT)
        print(summary(mc1))
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to set up my dev machine to run multiple VMs in parallel
I want to run multiple instances of apache on one single machine? How to
I want run a script as follows: runner: ssh 'java program &' ssh 'java
Using JXTA 2.6 from http://jxse.kenai.com/ I want to create application that can run multiple
I want to run multiple Python versions in my box. Is there any version
I want to run multiple versions of MATLAB (with standalone licenses) on a Windows
I have a situation where I want to run multiple EventMachines in Ruby -
I am working on a .NET Web Service and I want to run multiple
I want to run multiple Google Chrome application windows logged into the same web
I really love node.js but it´s really complicating when you want to run multiple

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.