[R] help to program my function

Rui Barradas ruipbarradas at sapo.pt
Tue Aug 7 23:59:02 CEST 2012


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 used

This 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.



More information about the R-help mailing list