I have a 3x3 matrix Omega whose elements are unknown, and a defined 3x1 parameter vector alpha. I want to define a 3x1 vector delta whose elements are still unknown but depend on alpha and Omega as shown in this image (sorry, but when I write in Latex format it doesn't appear formatted in my preview): https://www.dropbox.com/home/Public?preview=model.png The term in the brackets simplifies into a number K, so I wrote this function: Alpha=c(-0.248,1.092,-0.518) K=function(gamma1,gamma2,gamma3,gamma12,gamma23,gamma13){ (1+Alpha[1]*(Alpha[1]+Alpha[2]*gamma12/(gamma1*gamma2)+Alpha[3]*gamma13/(gamma1*gamma3)))^(-1/2) } gamma1, gamma2, gamma3 are the elements in the diagonal of the 3x3 matrix Omega, whereas gamma12, gamma13, gamma23 are the off-diagonal elements (each elements repeats itself twice, e.g. gamma12=gamma21). So, by putting 6 arbitrary values in K() I get the scalar. So far so clear. The rest I'm not sure about. I want R to return me a vector delta defined as shown above. How can I write a function that would perform this algebraic calculation, and return a 3x1 vector delta whose elements are the same unknowns as in Omega, but shifted/multiplied by the numbers in alpha? [[alternative HTML version deleted]]
> On 20 Jul 2016, at 19:20, Sachin Kuruvithadam <sacios at hotmail.it> wrote: > > > > I have a 3x3 matrix Omega whose elements are unknown, and a defined 3x1 parameter vector alpha. I want to define a 3x1 vector delta whose elements are still unknown but depend on alpha and Omega as shown in this image (sorry, but when I write in Latex format it doesn't appear formatted in my preview): > > https://www.dropbox.com/home/Public?preview=model.png >I cannot access your image.> The term in the brackets simplifies into a number K, so I wrote this function: > > Alpha=c(-0.248,1.092,-0.518) > K=function(gamma1,gamma2,gamma3,gamma12,gamma23,gamma13){ > (1+Alpha[1]*(Alpha[1]+Alpha[2]*gamma12/(gamma1*gamma2)+Alpha[3]*gamma13/(gamma1*gamma3)))^(-1/2) > } >This functions a scalar not a function> gamma1, gamma2, gamma3 are the elements in the diagonal of the 3x3 matrix Omega, whereas gamma12, gamma13, gamma23 are the off-diagonal elements (each elements repeats itself twice, e.g. gamma12=gamma21). So, by putting 6 arbitrary values in K() I get the scalar. So far so clear. > > The rest I'm not sure about. I want R to return me a vector delta defined as shown above. How can I write a function that would perform this algebraic calculation, and return a 3x1 vector delta whose elements are the same unknowns as in Omega, but shifted/multiplied by the numbers in alpha? >You can simplify the gamma arguments of your function K by passing the matrix Omega. Like this <code> K1 <- function(Omega,Alpha){ gamma1 <- Omega[1,1] gamma2 <- Omega[2,2] gamma3 <- Omega[3,3] gamma12 <- Omega[1,2] gamma13 <- Omega[1,2] gamma23 <- Omega[2,3] z <- 1+Alpha[1]*(Alpha[1]+Alpha[2]*gamma12/(gamma1*gamma2)+Alpha[3]*gamma13/(gamma1*gamma3))^(-1/2) z } </code> Define as given in your mail Alpha <- c(-0.248,1.092,-0.518) Fake a symmetric matrix Omega <code> set.seed(413: library(Matrix) x <- Matrix(round(runif(9),2), 3) x Omega <- forceSymmetric(x) Omega # and run the function K1(Omega,Alpha) </code> The result is a scalar: 0.8149383 Depending on the contents of Omega the result of K1 may be NaN. You will have to redefine your function if you want it to return a vector. You have not given enough information to give an answer to your question.> [[alternative HTML version deleted]] >Please do not post in HTML. Berend Hasselman> ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
> On 20 Jul 2016, at 20:07, Berend Hasselman <bhh at xs4all.nl> wrote: > > > > This functions a scalar not a functionCorrection. This should have been This function returns a scalar not a vector. Berend Hasselman
> On Jul 20, 2016, at 10:20 AM, Sachin Kuruvithadam <sacios at hotmail.it> wrote: > > > > I have a 3x3 matrix Omega whose elements are unknown, and a defined 3x1 parameter vector alpha. I want to define a 3x1 vector delta whose elements are still unknown but depend on alpha and Omega as shown in this image (sorry, but when I write in Latex format it doesn't appear formatted in my preview): > > https://www.dropbox.com/home/Public?preview=model.png > > The term in the brackets simplifies into a number K, so I wrote this function: > > Alpha=c(-0.248,1.092,-0.518) > K=function(gamma1,gamma2,gamma3,gamma12,gamma23,gamma13){ > (1+Alpha[1]*(Alpha[1]+Alpha[2]*gamma12/(gamma1*gamma2)+Alpha[3]*gamma13/(gamma1*gamma3)))^(-1/2) > } > > gamma1, gamma2, gamma3 are the elements in the diagonal of the 3x3 matrix Omega, whereas gamma12, gamma13, gamma23 are the off-diagonal elements (each elements repeats itself twice, e.g. gamma12=gamma21). So, by putting 6 arbitrary values in K() I get the scalar. So far so clear. > > The rest I'm not sure about. I want R to return me a vector delta defined as shown above.No. Not clear at all. What vector delta?> How can I write a function that would perform this algebraic calculation, and return a 3x1 vector delta whose elements are the same unknowns as in Omega, but shifted/multiplied by the numbers in alpha?That expression inside the K()-function would return a single value. I worry that you are expecting the Alpha's to retain their distinct bases as would happen if they were true 3-vectors.> > > [[alternative HTML version deleted]]Crossposting to Rhelp and StackOverflow at the same time is deprecated. This is the case on Rhelp, anyway. As far as I can tell SO has no such advice. Please read the Posting Guide where you should see that HTML formatted postings are discouraged and that most attachments are scrubbed by the email server. There has already been a comment-response on SO where another person reported difficulty accessing that image. You would be more likely to learn R coding if you used R code to define an Omega matrix with "dummy" values for testing and then tried passing it just as a square matrix to a function. ______________________________________________> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.David Winsemius Alameda, CA, USA