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

  • Home
  • SEARCH
  • 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 7757491
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T13:06:33+00:00 2026-06-01T13:06:33+00:00

I want to minimize a simple linear function Y = x1 + x2 +

  • 0

I want to minimize a simple linear function Y = x1 + x2 + x3 + x4 + x5 using ordinary least squares with the constraint that the sum of all coefficients have to equal 5. How can I accomplish this in R? All of the packages I’ve seen seem to allow for constraints on individual coefficients, but I can’t figure out how to set a single constraint affecting coefficients. I’m not tied to OLS; if this requires an iterative approach, that’s fine as well.

  • 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-06-01T13:06:34+00:00Added an answer on June 1, 2026 at 1:06 pm

    The basic math is as follows: we start with

    mu = a0 + a1*x1 + a2*x2 + a3*x3 + a4*x4
    

    and we want to find a0–a4 to minimize the SSQ between mu and our response variable y.

    if we replace the last parameter (say a4) with (say) C-a1-a2-a3 to honour the constraint, we end up with a new set of linear equations

    mu = a0 + a1*x1 + a2*x2 + a3*x3 + (C-a1-a2-a3)*x4
       = a0 + a1*(x1-x4) + a2*(x2-x4) + a3*(x3-x4) + C*x4
    

    (note that a4 has disappeared …)

    Something like this (untested!) implements it in R.

    1. Original data frame:

      d <- data.frame(y=runif(20),
                      x1=runif(20),
                      x2=runif(20),
                      x3=runif(20),
                      x4=runif(20))
      
    2. Create a transformed version where all but the last column have the last column “swept out”, e.g. x1 -> x1-x4; x2 -> x2-x4; ...

      dtrans <- data.frame(y=d$y,
                           sweep(d[,2:4],
                                 1,
                                 d[,5],
                                 "-"),
                           x4=d$x4)
      
    3. Rename to tx1, tx2, … to minimize confusion:

      names(dtrans)[2:4] <- paste("t",names(dtrans[2:4]),sep="")
      
    4. Sum-of-coefficients constraint:

      constr <- 5  
      
    5. Now fit the model with an offset:

      lm(y~tx1+tx2+tx3,offset=constr*x4,data=dtrans)
      

    It wouldn’t be too hard to make this more general.

    This requires a little more thought and manipulation than simply specifying a constraint to a canned optimization program. On the other hand, (1) it could easily be wrapped in a convenience function; (2) it’s much more efficient than calling a general-purpose optimizer, since the problem is still linear (and in fact one dimension smaller than the one you started with). It could even be done with big data (e.g. biglm). (Actually, it occurs to me that if this is a linear model, you don’t even need the offset, although using the offset means you don’t have to compute a0=intercept-C*x4 after you finish.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a WinForm that I want to minimize when the x in the
I have an application that launches Notepad. What I want to do is minimize
To speed up some unit tests that minimize and maximize windows, I want to
Rather than having a bunch of links that are all different sizes, I want
I'm using Visual Basic 2010 Express. I have a form that can be minimized.
I've written a python function that scours a filesystem using a provided directory pattern,
Greetings, I have a simple winform application of which I want to remove the
I have a bunch of repository classes which I want to unit-test using Visual
want to know why String behaves like value type while using ==. String s1
want to have a Hyperlink-Button in a gridView in which I can display a

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.