# [R] Fwd: Quadratic programming, for loop

Jeff Newmiller jdnewm|| @end|ng |rom dcn@d@v|@@c@@u@
Tue Jun 26 14:14:10 CEST 2018

```The recommended (see the Posting Guide) way to resolve questions like this is to post a reproducible example so we can see the problem occur in our R session. There are a number of Internet resources that can help you get this right such as [1][2][3].

Note that one key to success is to learn how to configure your email program send plain text formatted email, since the mailing list will strip the HTML formatting anyway but this often leaves extraneous characters that make no sense to R. Most email attachments are removed, so keep including the code in the email body as you have been.

[1] http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example

On June 26, 2018 1:34:31 AM PDT, "Maija Sirkjärvi" <maija.sirkjarvi using gmail.com> wrote:
>
>dvec, thus hsmooth, has the same length J. It shouldn't be the problem.
>
>2018-06-26 11:24 GMT+03:00 Eric Berger <ericjberger using gmail.com>:
>
>> The statement
>>
>> dvec <- -hsmooth
>>
>> looks like it might be the source of the problem, depending on what
>> hsmooth is.
>>
>>
>> On Tue, Jun 26, 2018 at 11:16 AM, Maija Sirkjärvi <
>> maija.sirkjarvi using gmail.com> wrote:
>>
>>> Thanks for the reply! I got that figured out, but still have some
>problems
>>>
>>> It seems that my Amat and dvec are incompatible. Amat is a matrix of
>zeros
>>> size: *2*J-3,J* and dvec is a vector of length *J*. There should be
>no
>>>
>>> problem, but apparently there is. The piece of code looks like this:
>>>
>>> Dmat <- matrix(0,nrow= J, ncol=J)
>>> diag(Dmat) <- 1
>>> dvec <- rep(0,J)
>>> dvec
>>> dvec <- -hsmooth
>>> Aeq <- 0
>>> beq <- 0
>>> Amat <- matrix(0,2*J-3,J)
>>> bvec <- rep(0,2*J-3)
>>>
>>> for(j in 2:J)
>>> {
>>> Amat[j-1,j-1] = -1
>>> Amat[j-1,j] = 1
>>> bvec[j-1] = Delta1
>>> }
>>>
>>> for(j in 3:J)
>>> {
>>> Amat[J-1+j-2,j] = -1/(Q[j] - Q[j-1])
>>> Amat[J-1+j-2,j-1]= 1/(Q[j] - Q[j-1]) + 1/(Q[j-1] - Q[j-2])
>>> Amat[J-1+j-2,j-2]= -1/(Q[j-1] - Q[j-2])
>>> bvec[J-1+j-2]= Delta2
>>> }
>>>
>>> solution <- solve.QP(Dmat, dvec, Amat, bvec)
>>>
>>>
>>> 2018-06-14 15:52 GMT+03:00 Boris Steipe <boris.steipe using utoronto.ca>:
>>>
>>> > Keep replies on list please.
>>> >
>>> > You are not accessing a value from vector Q if you access the
>zero'th
>>> > element!
>>> > R > Q <- c(3, 5, 8)
>>> > R > Q[0]
>>> > numeric(0)
>>> > R > Q[1]
>>> > [1] 3
>>> > R > Q[2]
>>> > [1] 5
>>> >
>>> > In the first iteration of the loop j is 2 thus j-2 is 0 and that's
>the
>>> > reason for the error message: you are trying to replace a matrix
>element
>>> > with a zero-length (i.e. unassigned) numeric value. Perhaps, in
>your
>>> mind,
>>> > you are mixing up the index of a vector element and its value? If
>you
>>> need
>>> > two zeros to start your vector, do something like
>>> >
>>> > R > Q <- c(0, 0, Q)
>>> > R > Q
>>> > [1] 0 0 3 5 8
>>> >
>>> >
>>> > Clear now?
>>> > B.
>>> >
>>> >
>>> >
>>> > > On 2018-06-14, at 01:22, Maija Sirkjärvi
><maija.sirkjarvi using gmail.com>
>>> > wrote:
>>> > >
>>> > > Many thanks for your message!
>>> > >
>>> > > The thing is that I need  Q[j-2] to be zero for the first two
>>> iterations
>>> > because I don't have those values (J starts from 1). Do you have
>any
>>> idea
>>> > how to do it?
>>> > >
>>> > > Thanks again!
>>> > >
>>> > > Maija
>>> > >
>>> > > 2018-06-13 15:52 GMT+03:00 Boris Steipe
><boris.steipe using utoronto.ca>:
>>> > > Q[j-2] gives you Q[0] in your first inner loop iteration.
>>> > > R arrays start at one.
>>> > >
>>> > > B.
>>> > >
>>> > >
>>> > > > On 2018-06-13, at 07:21, Maija Sirkjärvi
><maija.sirkjarvi using gmail.com
>>> >
>>> > wrote:
>>> > > >
>>> > > >  Amat[J-1+j-2,j-1]= 1/(Q[j] - Q[j-1]) + 1/(Q[j-1] - Q[j-2])
>>> > >
>>> > >
>>> >
>>> >
>>>
>>>         [[alternative HTML version deleted]]
>>>
>>> ______________________________________________
>>> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> ng-guide.html
>>> and provide commented, minimal, self-contained, reproducible code.
>>>
>>
>>
>
>	[[alternative HTML version deleted]]
>
>______________________________________________
>R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
>https://stat.ethz.ch/mailman/listinfo/r-help