HI>i have a problem please help me to solve it:http://r.789695.n4.nabble.com/file/n4639434/aj.pdf aj.pdf>i want to calculate the vecteur a[j] where j: 1...8>this is the code in R:>aj.fun <- function(j, i, X, z, E, beta0, beta1){+ n <- length(X) + iX <- order(X) + iz <- order(z) + e1 <- -(beta)*z[ iz[1:(i - 1)] ] + numer <- E[j] - sum( X[ iX[1:(i - 1)] ] * exp(e1) ) + e2 <- -(beta)*z[ iz[i:n] ] + denom <- sum( exp(e2) ) + numer/denom + }> iX<-order(X) > iX[1] 75 37 29 60 73 20 69 55 30 70 72 38 26 35 65 61 74 50 71 57 25 54 64 76 56 [26] 58 48 67 46 63 28 62 36 49 47 66 1 42 41 19 39 43 22 51 68 33 27 9 15 11 [51] 10 59 32 40 45 44 52 16 18 34 4 53 21 23 31 7 6 13 14 12 17 24 5 8 2 [76] 3> iZ<-order(Z) > iZ[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 [26] 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 [51] 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 [76] 76>e1 <- -(beta)*Z[ iZ[1:(i - 1)] ]Warning message: In 1:(i - 1) : numerical expression has 76 elements: only the first used> e1[1] -442 -1664>> numer <- E[j] - sum( X[ iX[1:(i - 1)] ] * exp(e1))Warning message: In 1:(i - 1) : numerical expression has 76 elements: only the first used> numer[1] 9.5 9.5 9.5 9.5 9.5 9.5 9.5 9.5>> e2 <- -(beta)*Z[ iZ[i:n] ]Warning message: In i:n : numerical expression has 76 elements: only the first used> e2[1] -442 -1664 -442 -1792 -476 -1792 -476 -1792 -510 -1920 -510 -1920 [13] -510 -1920 -510 -1920 -510 -1920 -510 -2048 -544 -2048 -544 -2048 [25] -544 -2048 -544 -2048 -544 -2048 -544 -2048 -544 -2048 -578 -2176 [37] -578 -2176 -578 -2176 -578 -2176 -578 -2176 -578 -2176 -578 -2176 [49] -578 -2176 -578 -2176 -578 -2304 -612 -2304 -612 -2304 -612 -2304 [61] -612 -2304 -612 -2304 -612 -2304 -612 -2304 -646 -2432 -646 -2432 [73] -646 -2432 -646 -2432> denom <- sum( exp(e2) ) > numer/denom[1] 4.313746e+192 4.313746e+192 4.313746e+192 4.313746e+192 4.313746e+192 [6] 4.313746e+192 4.313746e+192 4.313746e+192>my problem that the vecteur a[j] could not have the same number!!!thank you in advance hafida -- View this message in context: http://r.789695.n4.nabble.com/help-to-program-my-function-tp4639434.html Sent from the R help mailing list archive at Nabble.com.
Hello, Your problem is something else, before returning a vector of only the same values there are warning messages. Inline. Em 07-08-2012 18:14, hafida escreveu:> HI > >> i have a problem please help me to solve it: > http://r.789695.n4.nabble.com/file/n4639434/aj.pdf aj.pdf > >> i want to calculate the vecteur a[j] where j: 1...8 >> this is the code in R: >> aj.fun <- function(j, i, X, z, E, beta0, beta1){ > + n <- length(X) > + iX <- order(X) > + iz <- order(z) > + e1 <- -(beta)*z[ iz[1:(i - 1)] ] > + numer <- E[j] - sum( X[ iX[1:(i - 1)] ] * exp(e1) ) > + e2 <- -(beta)*z[ iz[i:n] ] > + denom <- sum( exp(e2) ) > + numer/denom > + } > >> iX<-order(X) >> iX > [1] 75 37 29 60 73 20 69 55 30 70 72 38 26 35 65 61 74 50 71 57 25 54 64 76 > 56 > [26] 58 48 67 46 63 28 62 36 49 47 66 1 42 41 19 39 43 22 51 68 33 27 9 15 > 11 > [51] 10 59 32 40 45 44 52 16 18 34 4 53 21 23 31 7 6 13 14 12 17 24 5 8 > 2 > [76] 3 > >> iZ<-order(Z) >> iZ > [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 > 25 > [26] 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 > 50 > [51] 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 > 75 > [76] 76 > >> e1 <- -(beta)*Z[ iZ[1:(i - 1)] ] > Warning message: > In 1:(i - 1) : numerical expression has 76 elements: only the first usedThis means that 'i' is a vector of length 76, which, in this context, is illegal. The variable 'i' must be a vector of only ONE value. If you want to compute the function for several (76) values do it one at a time, the function aj.fun is not vectorized. Hope this helps, Rui Barradas>> e1 > [1] -442 -1664 > >>> numer <- E[j] - sum( X[ iX[1:(i - 1)] ] * exp(e1)) > Warning message: > In 1:(i - 1) : numerical expression has 76 elements: only the first used >> numer > [1] 9.5 9.5 9.5 9.5 9.5 9.5 9.5 9.5 > >>> e2 <- -(beta)*Z[ iZ[i:n] ] > Warning message: > In i:n : numerical expression has 76 elements: only the first used >> e2 > [1] -442 -1664 -442 -1792 -476 -1792 -476 -1792 -510 -1920 -510 -1920 > [13] -510 -1920 -510 -1920 -510 -1920 -510 -2048 -544 -2048 -544 -2048 > [25] -544 -2048 -544 -2048 -544 -2048 -544 -2048 -544 -2048 -578 -2176 > [37] -578 -2176 -578 -2176 -578 -2176 -578 -2176 -578 -2176 -578 -2176 > [49] -578 -2176 -578 -2176 -578 -2304 -612 -2304 -612 -2304 -612 -2304 > [61] -612 -2304 -612 -2304 -612 -2304 -612 -2304 -646 -2432 -646 -2432 > [73] -646 -2432 -646 -2432 >> denom <- sum( exp(e2) ) >> numer/denom > [1] 4.313746e+192 4.313746e+192 4.313746e+192 4.313746e+192 4.313746e+192 > [6] 4.313746e+192 4.313746e+192 4.313746e+192 > >> my problem that the vecteur a[j] could not have the same number!!! > > > thank you in advance > hafida > > > > -- > View this message in context: http://r.789695.n4.nabble.com/help-to-program-my-function-tp4639434.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > 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.
Hi Maybe it is time for you to read some basic stuff like R intro. It seems to me that you expect R to behave like some other language you know but probably your expectation is wrong. See inline> > HI > > >i have a problem please help me to solve it: > http://r.789695.n4.nabble.com/file/n4639434/aj.pdf aj.pdf > > >i want to calculate the vecteur a[j] where j: 1...8 > > >this is the code in R: > > >aj.fun <- function(j, i, X, z, E, beta0, beta1){ > + n <- length(X) > + iX <- order(X) > + iz <- order(z) > + e1 <- -(beta)*z[ iz[1:(i - 1)] ]where do you get beta> + numer <- E[j] - sum( X[ iX[1:(i - 1)] ] * exp(e1) ) > + e2 <- -(beta)*z[ iz[i:n] ] > + denom <- sum( exp(e2) ) > + numer/denom > + } > > > iX<-order(X) > > iX > [1] 75 37 29 60 73 20 69 55 30 70 72 38 26 35 65 61 74 50 71 57 25 5464 76> 56 > [26] 58 48 67 46 63 28 62 36 49 47 66 1 42 41 19 39 43 22 51 68 33 27 915> 11 > [51] 10 59 32 40 45 44 52 16 18 34 4 53 21 23 31 7 6 13 14 12 17 24 58> 2 > [76] 3 > > > iZ<-order(Z) > > iZ > [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 2223 24> 25 > [26] 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 4748 49> 50 > [51] 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 7273 74> 75 > [76] 76 > > >e1 <- -(beta)*Z[ iZ[1:(i - 1)] ] > Warning message: > In 1:(i - 1) : numerical expression has 76 elements: only the first usedAs somebody already mentioned i is probably vector and in this case only first value is taken. i seems to have the firs value 3.> > e1 > [1] -442 -1664 > > >> numer <- E[j] - sum( X[ iX[1:(i - 1)] ] * exp(e1)) > Warning message: > In 1:(i - 1) : numerical expression has 76 elements: only the first used > > numer > [1] 9.5 9.5 9.5 9.5 9.5 9.5 9.5 9.5Here j is vector of 8 values therefore 8 values> > >> e2 <- -(beta)*Z[ iZ[i:n] ] > Warning message: > In i:n : numerical expression has 76 elements: only the first used > > e2 > [1] -442 -1664 -442 -1792 -476 -1792 -476 -1792 -510 -1920 -510-1920> [13] -510 -1920 -510 -1920 -510 -1920 -510 -2048 -544 -2048 -544-2048> [25] -544 -2048 -544 -2048 -544 -2048 -544 -2048 -544 -2048 -578-2176> [37] -578 -2176 -578 -2176 -578 -2176 -578 -2176 -578 -2176 -578-2176> [49] -578 -2176 -578 -2176 -578 -2304 -612 -2304 -612 -2304 -612-2304> [61] -612 -2304 -612 -2304 -612 -2304 -612 -2304 -646 -2432 -646-2432> [73] -646 -2432 -646 -2432Strange, here first value of i seems to be 1 as n shall be 76 and final e2 length is 76.> > denom <- sum( exp(e2) )> > numer/denom > [1] 4.313746e+192 4.313746e+192 4.313746e+192 4.313746e+1924.313746e+192> [6] 4.313746e+192 4.313746e+192 4.313746e+192 > > >my problem that the vecteur a[j] could not have the same number!!!I do not understand. Your numer is 9.5 repeted 8 times. If you divide it by one number you will get nine times the same number. You send us a code but no data so it is difficult to understand what is your goal. It would be better to send input data j, i, X, z, E, beta0, beta1 and assumed result in whole not in chunks scattered in several mails. Regards Petr> > > thank you in advance > hafida > > > > -- > View this message in context: http://r.789695.n4.nabble.com/help-to- > program-my-function-tp4639434.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.
hi peter the pdf folder is in http://r.789695.n4.nabble.com/file/n4639434/aj.pdf -- View this message in context: http://r.789695.n4.nabble.com/help-to-program-my-function-tp4639434p4639629.html Sent from the R help mailing list archive at Nabble.com.
I am not allowed to connect to Nabble from work, sorry. Petr> > hi peter the pdf folder is in > http://r.789695.n4.nabble.com/file/n4639434/aj.pdf > > > > -- > View this message in context: http://r.789695.n4.nabble.com/help-to- > program-my-function-tp4639434p4639629.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.