[R] System of equations with unknowns in R
Berend Hasselman
bhh at xs4all.nl
Wed Jul 20 20:07:30 CEST 2016
> 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.
More information about the R-help
mailing list