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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T20:12:11+00:00 2026-05-31T20:12:11+00:00

I am working with the output from a model in which there are parameter

  • 0

I am working with the output from a model in which there are parameter estimates that may not follow a-priori expectations. I would like to write a function that forces these utility estimates back in line with those expectations. To do this, the function should minimize the sum of the squared deviance between the starting values and the new estimates. Since we have a-priori expections, the optimization should be subject to the following constraints:

B0 < B1
B1 < B2
...
Bj < Bj+1 

For example, the raw parameter estimates below are flipflopped for B2 and B3. The columns Delta and Delta^2 show the deviance between the original parameter estimate and the new coefficient. I am trying to minimize the column Delta^2. I’ve coded this up in Excel and shown how Excel’s Solver would optimize this problem providing the set of constraints:

Beta    BetaRaw    Delta    Delta^2    BetaNew
B0       1.2       0        0          1.2
B1       1.3       0        0          1.3
B2       1.6       -0.2     0.04       1.4
B3       1.4       0        0          1.4
B4       2.2       0        0          2.2

After reading through ?optim and ?constrOptim, I’m not able to grok how to set this up in R. I’m sure I’m just being a bit dense, but could use some pointers in the right direction!

3/24/2012 – Added bounty since I’m not smart enough to translate the first answer.

Here’s some R code that should be on the right path. Assuming that the betas start with:

betas <- c(1.2,1.3,1.6,1.4,2.2)

I want to minimize the following function such that b0 <= b1 <= b2 <= b3 <= b4

f <- function(x) {
  x1 <- x[1]
  x2 <- x[2]
  x3 <- x[3]
  x4 <- x[4]
  x5 <- x[5]

 loss <- (x1 - betas[1]) ^ 2 + 
         (x2 - betas[2]) ^ 2 + 
         (x3 - betas[3]) ^ 2 + 
         (x4 - betas[4]) ^ 2 +
         (x5 - betas[5]) ^ 2    

  return(loss)
}

To show that the function works, the loss should be zero if we pass the original betas in:

> f(betas)
[1] 0

And relatively large with some random inputs:

> set.seed(42)
> f(rnorm(5))
[1] 8.849329

And minimized at the values I was able to calculate in Excel:

> f(c(1.2,1.3,1.4,1.4,2.2))
[1] 0.04
  • 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-31T20:12:13+00:00Added an answer on May 31, 2026 at 8:12 pm

    1.
    Since the objective is quadratic and the constraints linear,
    you can use solve.QP.

    It finds the b that minimizes

    (1/2) * t(b) %*% Dmat %*% b - t(dvec) %*% b 
    

    under the constraints

    t(Amat) %*% b >= bvec. 
    

    Here, we want b that minimizes

    sum( (b-betas)^2 ) = sum(b^2) - 2 * sum(b*betas) + sum(beta^2)
                       = t(b) %*% t(b) - 2 * t(b) %*% betas + sum(beta^2).
    

    Since the last term, sum(beta^2), is constant, we can drop it,
    and we can set

    Dmat = diag(n)
    dvec = betas.
    

    The constraints are

    b[1] <= b[2]
    b[2] <= b[3]
    ...
    b[n-1] <= b[n]
    

    i.e.,

    -b[1] + b[2]                       >= 0
          - b[2] + b[3]                >= 0
                   ...
                       - b[n-1] + b[n] >= 0
    

    so that t(Amat) is

    [ -1  1                ]
    [    -1  1             ]
    [       -1  1          ]
    [             ...      ]
    [                -1  1 ]
    

    and bvec is zero.

    This leads to the following code.

    # Sample data
    betas <- c(1.2, 1.3, 1.6, 1.4, 2.2)
    
    # Optimization
    n <- length(betas)
    Dmat <- diag(n)
    dvec <- betas
    Amat <- matrix(0,nr=n,nc=n-1)
    Amat[cbind(1:(n-1), 1:(n-1))] <- -1
    Amat[cbind(2:n,     1:(n-1))] <-  1
    t(Amat)  # Check that it looks as it should
    bvec <- rep(0,n-1)
    library(quadprog)
    r <- solve.QP(Dmat, dvec, Amat, bvec)
    
    # Check the result, graphically
    plot(betas)
    points(r$solution, pch=16)
    

    2.
    You can use constrOptim in the same way (the objective function can be arbitrary, but the constraints have to be linear).

    3.
    More generally, you can use optim if you reparametrize the problem
    into a non-constrained optimization problem,
    for instance

    b[1] = exp(x[1])
    b[2] = b[1] + exp(x[2])
    ...
    b[n] = b[n-1] + exp(x[n-1]).
    

    There are a few examples
    here
    or there.

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

Sidebar

Related Questions

Working on some tweaks for a build script, I noticed that the output from
I'm working on research model which outputs results to matlab's .mat file format, and
I`m working on an app that does reading and handling specific URIs from NFC
Is there a tool that works with SQL Server to generate tree-like diagrams from
I'm working with model output at the moment, and I can't seem to come
I'm working on a EMF project where I've created a ecore model from a
For single output types my code is working fine but I couldn't get how
Possible Duplicate: Optional Output Parameters I am currently working in a module wherein I
Working with regular javascript, I need to store info into an array for output
I am working on a hangman program and I want to output a status

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.