[R] Create a vector from another vector

Dimitris Rizopoulos dimitris.rizopoulos at med.kuleuven.be
Wed Aug 30 17:11:06 CEST 2006


maybe something like this could be of help:

max.score <- c(3,4,3) # max score for each item
all.pats <- as.matrix(expand.grid(lapply(max.score, ":", 1)))
all.pats[rowSums(all.pats) == 5, ]



Best,
Dimitris

----
Dimitris Rizopoulos
Ph.D. Student
Biostatistical Centre
School of Public Health
Catholic University of Leuven

Address: Kapucijnenvoer 35, Leuven, Belgium
Tel: +32/(0)16/336899
Fax: +32/(0)16/337015
Web: http://med.kuleuven.be/biostat/
     http://www.student.kuleuven.be/~m0390867/dimitris.htm


----- Original Message ----- 
From: "Doran, Harold" <HDoran at air.org>
To: "Duncan Murdoch" <murdoch at stats.uwo.ca>
Cc: <r-help at stat.math.ethz.ch>
Sent: Wednesday, August 30, 2006 4:49 PM
Subject: Re: [R] Create a vector from another vector


> Hi Duncan
>
> Here is a bit more detail, this is a bit tough to explain, sorry for 
> not
> being clear. Ordering is not important because the vector I am 
> creating
> is used as a sufficient statistic in an optimization routine to get 
> some
> MLEs. So, any combination of the vector that sums to X is OK. But, 
> the
> condition that x2[i] <= x[i] must be maintained. So, the example 
> below
> would not work because x2[1] > x[1] as you note below.
>
>> I don't think it's really clear what you mean by "ordering is
>> not important".  Would
>>
>> x2 <- c(6,5,2,4,2)
>> be acceptable (a re-ordering of your first two examples),
>> even though x2[1] > x1[1]?
>
> To be concrete, the following is the optimization function. This is 
> a
> psychometric problem where the goal is to get the MLE for a test 
> taker
> conditional on their response pattern (i.e., number of points on the
> test) and the item parameters.
>
> pcm.max3 <- function(score, d){
>    pcm <- function(theta, d, score)
> exp(sum(theta-d[1:score]))/sum(exp(cumsum(theta-d)))
>    opt <- function(theta) -sum(log(mapply(pcm, d, theta = theta, 
> score=
> score )))
>    start_val <- log(sum(score-1)/(length(score-1)/sum(score-1)))
>    out <- optim(start_val, opt, method = "BFGS", hessian = TRUE)
>    cat('theta is about', round(out$par, 2), ', se',
> 1/sqrt(out$hes),'\n')
>  }
>
> Suppose we have a three item test. I store the item parameters in a 
> list
> as
>
> items <- list(c(0,.5,1), c(0,1), c(0, -1, .5, 1))
>
> We can get the total possible number correct as
>
> (x <- sapply(items, length))
> [1] 3 2 4
>
> But, you cannot actually get the MLE for this because the likelihood 
> is
> unbounded in this case.
>
> So, let's say the student scored in the following categories for 
> each
> item:
>
> x2 <- c(3,1,4)
>
> By x2[i] <= x[i], I mean that there are 3 possible categories for 
> item 1
> above. So, a student can only score in categories 1,2 or 3. He 
> cannot
> score in category 4. This is why the condition that x2[i] <= x[i] is
> critical.
>
> But, because total score is a sufficient statistic, (i.e., "ordering 
> is
> not important") we could either vector in the function pcm.
>
> x3 <- c(3,2,3)
>
> Using the function
>
> pcm.max3(x2, items)
> pcm.max3(x3, items)
>
> Gives the same MLE.
>
> But, the vector
>
> X_bad <- c(4,1,3)
>
> Would not work. You can see that the elements of this vector 
> actually
> serve as indices denoting which category a test taker scored in for 
> each
> item in the list "items"
>
> I hope this is helpful and appreciate your time.
>
> Harold
>
>
>> >
>> > ______________________________________________
>> > 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
>> > and provide commented, minimal, self-contained, reproducible 
>> > code.
>>
>>
>
> ______________________________________________
> 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
> and provide commented, minimal, self-contained, reproducible code.
> 


Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm



More information about the R-help mailing list