[R] Basic vector operations was: Function to approximate complex integral
Doran, Harold
HDoran at air.org
Wed Apr 19 21:25:42 CEST 2006
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
More information about the R-help
mailing list