Doran, Harold
2006-Apr-19 19:25 UTC
[R] Basic vector operations was: Function to approximate complex integral
Dear List I apologize for the multiple postings. After being in the weeds on this problem for a while I think my original post may have been a little cryptic. I think I can be clearer. Essentially, I need the following a <- c(2,3) b <- c(4,5,6) (2*4) + (2*5) + (2*6) + (3*4) + (3*5) +(3*6) But I do not know of a built in function that would do this. Any suggestions? -----Original Message----- From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Doran, Harold Sent: Wednesday, April 19, 2006 11:51 AM To: r-help at stat.math.ethz.ch Subject: [R] Function to approximate complex integral I am writing a small function to approximate an integral that cannot be evaluated in closed form. I am partially successful at this point and am experiencing one small, albeit important problem. Here is part of my function below. This is a psychometric problem for dichotomously scored test items where x is a vector of 1s or 0s denoting whether the respondent answered the item correctly (1) or otherwise (0), b is a vector of item difficulties, and theta is an ability estimate for the individual. rasch <- function(b,theta){ 1 / ( 1 + exp(b - theta)) } The function rasch gives the probability of a correct response to item i conditional on theta, the individuals ability estimate myfun <- function(x, b, theta){ sum(rasch(b, theta)^x * ( 1 - rasch(b,theta) )^(1-x) * dnorm(theta)) } This is the likelihood function assuming the data are Bernoulli distributed multiplied by a population distribution. Now, when x,b, and theta are of equal length the function works fine as below x <- c(1,1,0) b <- c(-2,-1,0) t <- c(-2,-1.5,-1)> myfun(x,b,t)[1] 0.2527884 However, I want theta to be a vector of discrete values that will be larger than both x and b, something like t <- seq(-5, 0, by = .01) However, this gives me the error> myfun(x,b,t)Warning messages: 1: longer object length is not a multiple of shorter object length in: b - theta So, for the problem above, I want item 1 to be evaluated at theta 1 through theta q and then item 2 is evaluated at theta 1 and through theta q and so forth for each item. Can anyone recommend a way for me to modify my function above to accomplish this? Harold [[alternative HTML version deleted]] ______________________________________________ R-help at stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
jim holtman
2006-Apr-19 19:31 UTC
[R] Basic vector operations was: Function to approximate complex integral
You can always write your own function that will take the values, multiply and then sum them:> a <- c(2,3) > b <- c(4,5,6) > total <- 0 > for (i in a) total <- sum(total, i * b) > total[1] 75>On 4/19/06, Doran, Harold <HDoran@air.org> wrote:> > Dear List > > I apologize for the multiple postings. After being in the weeds on this > problem for a while I think my original post may have been a little > cryptic. I think I can be clearer. Essentially, I need the following > > a <- c(2,3) > b <- c(4,5,6) > > (2*4) + (2*5) + (2*6) + (3*4) + (3*5) +(3*6) > > But I do not know of a built in function that would do this. Any > suggestions? > > -----Original Message----- > From: r-help-bounces@stat.math.ethz.ch > [mailto:r-help-bounces@stat.math.ethz.ch] On Behalf Of Doran, Harold > Sent: Wednesday, April 19, 2006 11:51 AM > To: r-help@stat.math.ethz.ch > Subject: [R] Function to approximate complex integral > > > > I am writing a small function to approximate an integral that cannot be > evaluated in closed form. I am partially successful at this point and am > experiencing one small, albeit important problem. Here is part of my > function below. > > This is a psychometric problem for dichotomously scored test items where > x is a vector of 1s or 0s denoting whether the respondent answered the > item correctly (1) or otherwise (0), b is a vector of item difficulties, > and theta is an ability estimate for the individual. > > rasch <- function(b,theta){ > 1 / ( 1 + exp(b - theta)) > } > > The function rasch gives the probability of a correct response to item i > conditional on theta, the individuals ability estimate > > myfun <- function(x, b, theta){ > sum(rasch(b, theta)^x * ( 1 - rasch(b,theta) )^(1-x) * dnorm(theta)) > } > > This is the likelihood function assuming the data are Bernoulli > distributed multiplied by a population distribution. > > Now, when x,b, and theta are of equal length the function works fine as > below x <- c(1,1,0) > b <- c(-2,-1,0) > t <- c(-2,-1.5,-1) > > myfun(x,b,t) > [1] 0.2527884 > > However, I want theta to be a vector of discrete values that will be > larger than both x and b, something like > > t <- seq(-5, 0, by = .01) > > However, this gives me the error > > myfun(x,b,t) > > Warning messages: > 1: longer object length > is not a multiple of shorter object length in: b - theta > > So, for the problem above, I want item 1 to be evaluated at theta 1 > through theta q and then item 2 is evaluated at theta 1 and through > theta q and so forth for each item. > > Can anyone recommend a way for me to modify my function above to > accomplish this? > > Harold > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html > > ______________________________________________ > R-help@stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html >-- Jim Holtman Cincinnati, OH +1 513 646 9390 (Cell) +1 513 247 0281 (Home) What the problem you are trying to solve? [[alternative HTML version deleted]]
Sebastian Luque
2006-Apr-19 19:43 UTC
[R] Basic vector operations was: Function to approximate complex integral
"Doran, Harold" <HDoran at air.org> wrote: [...]> a <- c(2,3) > b <- c(4,5,6)> (2*4) + (2*5) + (2*6) + (3*4) + (3*5) +(3*6)> But I do not know of a built in function that would do this. Any > suggestions?How about: sum(a %o% b) ?%o% Cheers, -- Seb
Christos Hatzis
2006-Apr-19 19:45 UTC
[R] Basic vector operations was: Function to approximatecomplex integral
You get the same result with sum(outer(a,b)) -Christos -----Original Message----- From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of jim holtman Sent: Wednesday, April 19, 2006 3:32 PM To: Doran, Harold Cc: r-help at stat.math.ethz.ch Subject: Re: [R] Basic vector operations was: Function to approximatecomplex integral You can always write your own function that will take the values, multiply and then sum them:> a <- c(2,3) > b <- c(4,5,6) > total <- 0 > for (i in a) total <- sum(total, i * b) total[1] 75>On 4/19/06, Doran, Harold <HDoran at air.org> wrote:> > Dear List > > I apologize for the multiple postings. After being in the weeds on this > problem for a while I think my original post may have been a little > cryptic. I think I can be clearer. Essentially, I need the following > > a <- c(2,3) > b <- c(4,5,6) > > (2*4) + (2*5) + (2*6) + (3*4) + (3*5) +(3*6) > > But I do not know of a built in function that would do this. Any > suggestions? > > -----Original Message----- > From: r-help-bounces at stat.math.ethz.ch > [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Doran, Harold > Sent: Wednesday, April 19, 2006 11:51 AM > To: r-help at stat.math.ethz.ch > Subject: [R] Function to approximate complex integral > > > > I am writing a small function to approximate an integral that cannot be > evaluated in closed form. I am partially successful at this point and am > experiencing one small, albeit important problem. Here is part of my > function below. > > This is a psychometric problem for dichotomously scored test items where > x is a vector of 1s or 0s denoting whether the respondent answered the > item correctly (1) or otherwise (0), b is a vector of item difficulties, > and theta is an ability estimate for the individual. > > rasch <- function(b,theta){ > 1 / ( 1 + exp(b - theta)) > } > > The function rasch gives the probability of a correct response to item i > conditional on theta, the individuals ability estimate > > myfun <- function(x, b, theta){ > sum(rasch(b, theta)^x * ( 1 - rasch(b,theta) )^(1-x) * dnorm(theta)) > } > > This is the likelihood function assuming the data are Bernoulli > distributed multiplied by a population distribution. > > Now, when x,b, and theta are of equal length the function works fine as > below x <- c(1,1,0) > b <- c(-2,-1,0) > t <- c(-2,-1.5,-1) > > myfun(x,b,t) > [1] 0.2527884 > > However, I want theta to be a vector of discrete values that will be > larger than both x and b, something like > > t <- seq(-5, 0, by = .01) > > However, this gives me the error > > myfun(x,b,t) > > Warning messages: > 1: longer object length > is not a multiple of shorter object length in: b - theta > > So, for the problem above, I want item 1 to be evaluated at theta 1 > through theta q and then item 2 is evaluated at theta 1 and through > theta q and so forth for each item. > > Can anyone recommend a way for me to modify my function above to > accomplish this? > > Harold > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html >-- Jim Holtman Cincinnati, OH +1 513 646 9390 (Cell) +1 513 247 0281 (Home) What the problem you are trying to solve? [[alternative HTML version deleted]] ______________________________________________ R-help at stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Marc Schwartz (via MN)
2006-Apr-19 19:46 UTC
[R] Basic vector operations was: Function to approximate complex integral
On Wed, 2006-04-19 at 15:25 -0400, Doran, Harold wrote:> Dear List > > I apologize for the multiple postings. After being in the weeds on this > problem for a while I think my original post may have been a little > cryptic. I think I can be clearer. Essentially, I need the following > > a <- c(2,3) > b <- c(4,5,6) > > (2*4) + (2*5) + (2*6) + (3*4) + (3*5) +(3*6) > > But I do not know of a built in function that would do this. Any > suggestions?<SNIP> Unless I am missing something, how about:> sum(a %x% b)[1] 75 See ?"%x%" You could also use outer():> sum(outer(a, b, "*"))[1] 75 See ?outer HTH, Marc Schwartz