#### Simulate dyadic data
set.seed(123)
nind <- 10 # Beware data grow as O(nind^2)
x <- runif(nind^2)
id12 <- expand.grid(id1=seq(nind),id2=seq(nind))
id1 <- id12$id1
id2 <- id12$id2
u <- rnorm(nind,mean = 0, sd=0.5)
## additive individual effects:
y <- 0.1 + 1*x + u[id1] + u[id2] + rnorm(nind^2,sd=0.2)
## anti-smmetric individual effects:
t <- 0.1 + 1*x + u[id1] - u[id2] + rnorm(nind^2,sd=0.2)
dyaddf <- data.frame(x=x, y=y, t=t, id1=id1,id2=id2)
# : note that this contains two rows per dyad, which avoids identifiability issues.
# Enforce that interactions are between distinct individuals (not essential for the fit):
dyaddf <- dyaddf[- seq.int(1L,nind^2,nind+1L),]
# Fits:
(addfit <- fitme(y ~x +X.GCA(id1:id2), data=dyaddf))
(antifit <- fitme(t ~x +X.antisym(id1:id2), data=dyaddf))
if (FALSE) { #### check of correct handling by predict():
# First scramble the data so that input factors are in no particular order
set.seed(123)
dyaddf <- dyaddf[sample(nrow(dyaddf)),]
addfiti <- fitme(y ~x +X.GCA(id1:id2), data=dyaddf)
foo <- rev(2:4)
p1 <- predict(addfiti)[foo]
p2 <- predict(addfiti, newdata=dyaddf[foo,])
diff(range(p1-p2))<1e-10 # must be TRUE
}
Run the code above in your browser using DataLab