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 7623589
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T04:44:40+00:00 2026-05-31T04:44:40+00:00

In the following example, say you have a model where supp is a factor

  • 0

In the following example, say you have a model where supp is a factor variable.

lm(len ~ dose + supp, data = ToothGrowth)

but I want to use different base level for the factor. I could specify this directly in the formula:

lm(len ~ dose + relevel(supp, "VC"), data = ToothGrowth)

and the output would be:

Call:
lm(formula = len ~ dose + relevel(supp, "VC"), data = ToothGrowth)

Coefficients:
      (Intercept)                   dose  relevel(supp, "VC")OJ  
            5.573                  9.764                  3.700 

It is very convenient to do transformations directly in the formula, and not make intermediate data sets or alter the existing one. An example is when you use scale to standardize variables where it is essential to account for missings in other variables included in the final model. Often, however the resulting coefficient names in the output becomes quite ugly.

My question is: is it possible to specify the name of a variable which is resulting from an expression when working with the formula? Something like

lm(len ~ dose + (OJ = relevel(supp, "VC")), data = Toothgrowth)

(which does not work).

EDIT: While the solution proposed by G. Grothendieck is nice it actually produces the wrong result. The following example shows this:

# Create some data:
df <- data.frame(x1 = runif(10), x2=runif(10))
df <- transform(df,   y = x1 + x2 + rnorm(10))

# Introduce some missings.
df$x1[2:3] <- NA

# The wrong result:
lm(formula = y ~ z1 + z2, 
   data    = transform(df, z1 = scale(x1), z2=scale(x2)))

# extract a model frame.
df2 <- model.frame(y ~ x1 + x2, df)

# The right result:
lm(formula = y ~ scale(x1) + scale(x2), 
   data    = df2)

# or:
lm(formula = y ~ z1 + z2, 
   data    = transform(model.frame(y ~ x1 + x2, df), 
             z1 = scale(x1), z2 = scale(x2)))

The issue is that when demeaning x2, it uses observations that are not in the final model because x1 has missings.

So to me the question remains, whether there is a way for the formula interface to handle this case without having the annoying intermediate step of using an extra formula and extracting a model frame, which can then be “transformed”.

I hope the question is clear.

  • 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-31T04:44:41+00:00Added an answer on May 31, 2026 at 4:44 am

    Modify it in the data= argument rather than in the formula= argument:

    lm(len ~ dose + OJ, data = transform(ToothGrowth, OJ = relevel(supp, "VC")))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have the following two classes: package example.model; public class Model {
I'm using MVVM in WPF in C#.NET. Say I have the following example model:
Say I have the following code: <div onclick='location.href=http://www.example.com/'> <a href='#' onclick='alert(blah)'>click</a> </div> Is there
Say for example I have the following string: var testString = Hello, world; And
Say suppose I have the following Java code. public class Example { public static
Say for example, I have the following two tables: TableA { _id } TableB
My simplified and contrived example is the following:- Lets say that I want to
Given the following example, why do I have to explicitly use the statement b->A::DoSomething()
I have the following contrived example in Rails. I want to make sure the
Let's say I have the following: class Employee(models.Model): firstName = models.CharField(max_length = 30) lastName

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.