[Rsiena-help] structural zeros

Ruth M. Ripley ruth at stats.ox.ac.uk
Wed Jun 15 18:07:37 CEST 2011


Dear Sebastian,

You need to specify each individual link that is structural: eg 9 to 
all of 1 to 276 (except 9).

If you wait till you have created the sparse matrix, you can do

mymat2s[9, ] <- 10

to fill the row and

mymat2s[9, 11] <- 10

would fill the column if you do not want to allow incoming links either.

For safety, remove the diagonal:

mymat2s[9, 9] <- 0

or to do more than 1 at once

struct <- c(9, 13)

mymat2s[struct, ] <- 10

mymat2s[, struct] <- 10

diag(mymat2s) <- 0

BTW, the code I gave you will only work if each of the SPSS files has the 
same actor ids in the first column. I think yours did.

Regards,

Ruth

On Tue, 14 Jun 2011, Sebastián Daza wrote:

> Thank you Ruth. 3 NAs are isolates. I have additional information about 
> structural zeros (people who leave the network). For instance, this is the 
> edgelist for time 2 where nodes 9 and 13 are structural zeros (they have 
> valid ties in the time 1 edgelist).
>
> mymat2
>      [,1] [,2] [,3]
> [1,]    2   14    1
> [2,]    2    6    1
> [3,]    3    8    1
> [4,]    3    7    1
> [5,]    4    3    1
> [6,]    5   11    1
> [7,]    6    2    1
> [8,]    6   28    1
> [9,]    7    3    1
> [10,]    8    3    1
> [11,]    9   NA   10
> [12,]   10   24    1
> [13,]   10    6    1
> [14,]   11    5    1
> [15,]   13   NA   10
>
> I excluded all NAs except for structural zeros.  When I use this matrix 
> syntax, however, I got the following error:
>
> library(Matrix)
>
>
>> mymat2s <- spMatrix(nrow(friend2), nrow(friend2),
> + 		mymat2[, 1], mymat2[, 2], mymat2[, 3])
> Error in validObject(.Object) :
>  invalid class "dgTMatrix" object: all column indices must be between 0 and 
> ncol-1
>
> I do not know what I am doing wrong. I would really appreciate if you could 
> give any example of this that I could follow.
>
> Thank you again!
> Sebastian
>
>
> On 6/14/2011 5:24 PM, Ruth M. Ripley wrote:
>> Dear Sebastian,
>> 
>> I assumed rows with 3 NA's were isolates and there were no structural
>> zeros. But maybe isolates were the missing sequence numbers and the 3
>> NA's were structural zeros?
>> 
>> Structural zeros need 10 in column 3 of the edgelist.
>> 
>> Isolates need to be allowed for in the sequence numbering but will not
>> appear in the edgelist.
>> 
>> Regards,
>> 
>> Ruth
>> 
>> On Tue, 14 Jun 2011, Sebastián Daza wrote:
>> 
>>> Dear Ruth,
>>> Using your syntax to build matrices, I have a question about how to
>>> define structural zeros and how isolated nodes (vertexes) are stored
>>> in the edgelist format.
>>> 
>>> My original dataset (friends) has this format:
>>> 
>>> id tie1 tie2 tie3
>>> 1 3984 NA NA NA
>>> 2 3985 3997 NA 3989
>>> 3 3986 3991 3990 NA
>>> 4 3987 3986 NA NA
>>> 5 3988 3994 NA NA
>>> 6 3989 3985 4011 NA
>>> ....
>>> 
>>> your syntax to build the edgelist is:
>>> 
>>> mymat1 <- matrix(0, nrow=3*nrow(friend1), ncol=3)
>>> mymat1[, 1] <- rep(1:nrow(friend1), each=3)
>>> mymat1[, 2] <- match(t(as.matrix(friend1[, -1])), friend1[, 1])
>>> mymat1[, 3] <- rep(1, nrow(mymat1))
>>> mymat1 <- na.omit(mymat1)
>>> mymat1 <- unique(mymat1)
>>> 
>>> I am not sure how I should define the structural zeros (using the code
>>> 10), taking into account the structure of this edgelist:
>>> 
>>> [,1] [,2] [,3]
>>> [1,] 2 14 1
>>> [2,] 2 6 1
>>> [3,] 3 8 1
>>> [4,] 3 7 1
>>> [5,] 4 3 1
>>> [6,] 5 11 1
>>> 
>>> 
>>> I guess I should define the structural zero in the third column, but
>>> because I omitted the NAs, I can not do it. In addition, I don not
>>> know where the isolated nodes are stores in this format (for example,
>>> node 1).
>>> 
>>> Thank you in advance, and sorry for these very basic questions.
>>> Sebastian.
>>> 
>>> On 6/13/2011 1:40 PM, Ruth M. Ripley wrote:
>>>> Dear Sebastian,
>>>> 
>>>> Glad you got things to work.
>>>> 
>>>> I don't think these messages are important, and I thought they always
>>>> appeared, unrelated to covariates. You are not using sparse matrices to
>>>> improve performance at this stage: you only read in your data once and
>>>> will spend much more time fitting models. It is more a matter of
>>>> convenience.
>>>> 
>>>> Regards,
>>>> 
>>>> Ruth
>>>> 
>>>> 
>>>> On Mon, 13 Jun 2011, Sebastián Daza wrote:
>>>> 
>>>>> Thank you very much, Ruth. It works great!
>>>>> 
>>>>> However, when I include a covariate (e.g. sex) I get these messages:
>>>>> 
>>>>> <sparse>[ <logic> ] : .M.sub.i.logical() maybe inefficient
>>>>> <sparse>[ <logic> ] : .M.sub.i.logical() maybe inefficient
>>>>> 
>>>>> Sebastian
>>>>> 
>>>>> On 6/11/2011 5:41 PM, Ruth M. Ripley wrote:
>>>>>> Dear Sebastian,
>>>>>> 
>>>>>> After looking at your data, I thought I would reply to the list as
>>>>>> others may want to know the outcome:
>>>>>> 
>>>>>> I discovered there is a single 2 in the second matrix. I am not quite
>>>>>> sure of your data format but I think you have a duplicate in row
>>>>>> 188 of
>>>>>> friend2 which results in 2 links from 1706 to 1704.
>>>>>> 
>>>>>> For the benefit of others, your data is, I think, a row for each actor
>>>>>> with 3 nominations, NA if less than three, in an SPSS file. The actor
>>>>>> numbers are not sequential.
>>>>>> 
>>>>>> I have not tried reading the files in SPSS, but if all you want in
>>>>>> your
>>>>>> data is an integer 4 column matrix, I would think it easier to export
>>>>>> from SPSS as a text file, if that is possible.
>>>>>> 
>>>>>> I advise reading SPSS files using read.spss directly: I am not sure
>>>>>> you
>>>>>> gain anything by using Hmisc's wrapper version, but it seemed to
>>>>>> work OK
>>>>>> here. read.spss is in library(foreign).
>>>>>> 
>>>>>> But once you have read the files in to create two data frames friend1
>>>>>> and friend2, you could use siena edgelist format via sparse
>>>>>> matrices or
>>>>>> create normal matrices via the sparse ones, using the following code:
>>>>>> 
>>>>>> mymat1 <- matrix(0, nrow=3*nrow(friend1), ncol=3)
>>>>>> mymat1[, 1] <- rep(1:nrow(friend1), each=3)
>>>>>> mymat1[, 2] <- match(t(as.matrix(friend1[, -1])), friend1[, 1])
>>>>>> mymat1[, 3] <- rep(1, nrow(mymat1))
>>>>>> mymat1 <- na.omit(mymat1)
>>>>>> mymat1 <- unique(mymat1)
>>>>>> 
>>>>>> mymat2 <- matrix(0, nrow=3*nrow(friend2), ncol=3)
>>>>>> mymat2[, 1] <- rep(1:nrow(friend2), each=3)
>>>>>> mymat2[, 2] <- match(t(as.matrix(friend2[, -1])), friend2[, 1])
>>>>>> mymat2[, 3] <- rep(1, nrow(mymat2))
>>>>>> mymat2 <- na.omit(mymat2)
>>>>>> mymat2 <- unique(mymat2)
>>>>>> 
>>>>>> #To get RSiena networks using sparse matrices:
>>>>>> 
>>>>>> library(Matrix)
>>>>>> mymat1s <- spMatrix(nrow(friend1), nrow(friend1),
>>>>>> mymat1[, 1], mymat1[, 2], mymat1[, 3])
>>>>>> 
>>>>>> mymat2s <- spMatrix(nrow(friend2), nrow(friend2),
>>>>>> mymat2[, 1], mymat2[, 2], mymat2[, 3])
>>>>>> 
>>>>>> mynet <- sienaNet(list(mymat1s, mymat2s))
>>>>>> 
>>>>>> # to get real matrices
>>>>>> 
>>>>>> mymat1d <- as.matrix(mymat1s)
>>>>>> mymat2d <- as.matrix(mymat
>>>>>> 
>>>>>> ## if you wish add IDs as row numbers to the matrices
>>>>>> 
>>>>>> rownames(mymat1d) <- friend1[, 1]
>>>>>> rownames(mymat2d) <- friend2[, 1]
>>>>>> rownames(mymat1s) <- friend1[, 1]
>>>>>> rownames(mymat2s) <- friend2[, 1]
>>>>>> 
>>>>>> ## and add IDs as column numbers
>>>>>> colnames(mymat1d) <- friend1[, 1]
>>>>>> colnames(mymat2d) <- friend2[, 1]
>>>>>> colnames(mymat1s) <- friend1[, 1]
>>>>>> colnames(mymat2s) <- friend2[, 1]
>>>>>> 
>>>>>> Please let me know if you do not understand the code or cannot get
>>>>>> it to
>>>>>> work. I think it is simpler than your method, and should give the same
>>>>>> result apart from the duplicates.
>>>>>> 
>>>>>> Regards,
>>>>>> 
>>>>>> Ruth
>>>>>> --
>>>>>> On Sat, 11 Jun 2011, Ruth M. Ripley wrote:
>>>>>> 
>>>>>>> Dear Sebastian,
>>>>>>> 
>>>>>>> If you would like to email me the data in some format (either save
>>>>>>> the
>>>>>>> workspace and send that, or send me the data files and the commands
>>>>>>> you used to make the matrices) I will have a look for you.
>>>>>>> 
>>>>>>> Regards,
>>>>>>> 
>>>>>>> Ruth
>>>>>>> 
>>>>>>> 
>>>>>>> On Sat, 11 Jun 2011, Sebastián Daza wrote:
>>>>>>> 
>>>>>>>> Hello,
>>>>>>>> I am trying to load two 276x276 matrices using sienaNet, but I got
>>>>>>>> this error. Both files are matrices (matrix class), and they have
>>>>>>>> only 0s and 1s.
>>>>>>>> 
>>>>>>>>> friendship <- sienaNet( array( c( mnet1, mnet2),
>>>>>>>> + dim = c( 276, 276, 2 ) ) )
>>>>>>>> Error in sienaNet(array(c(mnet1, mnet2), dim = c(276, 276, 2))) :
>>>>>>>> entries in networks must be 0, 1, 10 or 11
>>>>>>>> 
>>>>>>>> Does anyone know what could be the problem?
>>>>>>>> Thank you in advance.
>>>>>>>> Sebastian.
>>>>>>>> 
>>>>>>>> --
>>>>>>>> Sebastián Daza
>>>>>>>> sebastian.daza at gmail.com
>>>>>>>> _______________________________________________
>>>>>>>> Rsiena-help mailing list
>>>>>>>> Rsiena-help at lists.r-forge.r-project.org
>>>>>>>> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rsiena-help
>>>>>>>> 
>>>>>>>> 
>>>>>>>> 
>>>>>>> 
>>>>> 
>>>>> --
>>>>> Sebastián Daza
>>>>> sebastian.daza at gmail.com
>>>>> 
>>>>> 
>>> 
>>> --
>>> Sebastián Daza
>>> sebastian.daza at gmail.com
>>> 
>>> 
>
> -- 
> Sebastián Daza
> sebastian.daza at gmail.com
>
>


More information about the Rsiena-help mailing list