# [R] Create a vector from another vector

Doran, Harold HDoran at air.org
Wed Aug 30 16:49:56 CEST 2006

```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

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"

Harold

> >
> > ______________________________________________
> > R-help at stat.math.ethz.ch mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help