[R] KLdiv question (data.frame)

Peter Ehlers ehlers at ucalgary.ca
Fri Jul 16 01:21:18 CEST 2010


On 2010-07-15 15:15, Ralf B wrote:
> Hi all,
>
> I wonder why KLdiv does not work with data.frames:

Because it's not designed that way?
(I assume that you mean KLdiv() in pkg:flexmix.)

Check the help page. It's pretty clear about what input
is wanted: a matrix object or a flexmix object (or FLXMC
or FLXMRglm). So, no method for a dataframe.

(BTW, you don't need those 'c()'s in your definition of
mydata.)

>
> n<- 50
> mydata<- data.frame(
> 	sequence=c(1:n),
> 	data1=c(rnorm(n)),
> 	data2=c(rnorm(n))
> )
> # does NOT work
> KLdiv(mydata)
> # works fine
> dataOnly<- cbind(mydata$data1, mydata$data2, mydata$group)
> KLdiv(dataOnly)
>
>
> Any ideas? Is there a better implementation that can deal with
> data.frame or is there a simpler way of converting?

What's wrong with formatting your input to KLdiv as a matrix?
If you want to convert your dataframe to a matrix, you could
use as.matrix(), but be warned: any non-numeric columns will
cause trouble.

   -Peter Ehlers



More information about the R-help mailing list