[R] Problem creation tensor
Petr Savicky
savicky at cs.cas.cz
Wed Jul 18 11:59:00 CEST 2012
On Wed, Jul 18, 2012 at 11:38:59AM +0200, Petr Savicky wrote:
> On Tue, Jul 17, 2012 at 12:31:38PM +0200, Peppe Ricci wrote:
> > Hi guys,
> >
> > I need some help to analyzing my data.
> > I start to describe my data: I have 21 matrices, every matrix on the
> > rows has users and on columns has items, in my case films.
> > Element of index (i, j) represent the rating expressed by user i about item j.
> > I have a matrix for each of professions.
> > An example of a this type of matrix is:
> >
> > item 1 item 2 item 3 item4
> > id user 1 1 ? ? 5
> > id user 2 ? 3 3 ?
> > id user 3 2 ? 3 2
> > id user 4 ? ? ? 4
> > ...
> > So user 1 don't like item 1 but he likes so much item 4, for item 2
> > and 3 he hasn't expressed a rating, etc.
> > I need to construct a tensor with n users, m items and 21 occupations.
> > After I have construct the tensor I want apply Parafac.
> > I read data from a CSV file and build each matrix for each occupation.
> >
[...]
> > I have implemented this code but I have one error in correspondance of:
> >
> > for ( i in 1:m){
> > Z[i,,]=table(occ,UserItem[,i])
> > }
> >
> > and error is:
> >
> > Error in
> > Z[i,,]=table(occ,UserItem[,i])
> > the number of elements to be replaced is not a multiple of the length
> > of substitution
>
> Hi.
>
> The problem in this code is that the command
>
> UserItem <- rbind(M1, M2, ..., M21)
>
> produces a matrix and not a data.frame. Due to this, the commands
Hi.
Let me include a few more comments. The function rbind() preserves the
types "matrix" and "data.frame". So, if Mi are indeed matrices, then
the above applies.
> UserItem[, j] <- factor(UserItem[, j], levels=1:5)
>
> do not convert the columns to factors, but the columns remain numeric.
> Due to this, the table created as
>
> table(occ, UserItem[, i])
>
> may not have the full size, since the columns correspond only to
> preferences, which do occur in UserItem[, i], and not to all possible
> preferences.
This can be demonstrated by the following example.
occ <- 1:3
pref <- c(1, 3, 4)
table(occ, pref)
pref
occ 1 3 4
1 1 0 0
2 0 1 0
3 0 0 1
and
table(occ, pref=factor(pref, levels=1:5))
pref
occ 1 2 3 4 5
1 1 0 0 0 0
2 0 0 1 0 0
3 0 0 0 1 0
Hope this helps.
Petr Savicky.
More information about the R-help
mailing list