library(randnet)
library(RSpectra)
### data generating procedure in Section 5.3 of the paper
n <- 1000
big.model <- BlockModel.Gen(lambda=n^(1/2),n=n,beta=0.2,K=4)
P <- big.model$P
big.X <- cbind(rnorm(n),runif(n),rexp(n))
eigen.P <- eigs_sym(A=P,k=4)
X.true <- big.X
X.true <- scale(X.true,center=TRUE,scale=TRUE)*sqrt(n/(n-1))
X.true <- cbind(sqrt(n)*eigen.P$vectors[,1],X.true)
X.svd <- svd(X.true)
x.proj <- X.svd$v%*%(t(X.svd$u)/X.svd$d)
Theta <- X.svd$v%*%(t(X.svd$v)/(X.svd$d^2))*n
R <- X.svd$u
U <- eigen.P$vectors[,1:4]
true.SVD <- svd(t(R)%*%U,nu=4,nv=4)
V <- true.SVD$v
r <- 1
U.tilde <- U%*%V
R.tilde <- R%*%true.SVD$u
theta.tilde <- matrix(c(sqrt(n),0,0,0),ncol=1)
beta.tilde <- matrix(sqrt(n)*c(0,1,1,1),ncol=1)
Xtheta <- R.tilde%*%theta.tilde
Xbeta <- R.tilde%*%beta.tilde
theta <- solve(t(X.true)%*%X.true,t(X.true)%*%Xtheta)
beta <- solve(t(X.true)%*%X.true,t(X.true)%*%Xbeta)
alpha.coef <- matrix(sqrt(n)*c(0,1,1,1),ncol=1)
alpha <- U.tilde%*%alpha.coef
EY <- Xtheta+Xbeta + alpha
#### model fitting
A <- net.gen.from.P(P)
Khat <- BHMC.estimate(A, K.max = 15)$K ### estimate K to use
## model fitting
Y <- EY + rnorm(n)
fit <- SP.Inf(X.true,Y,A,K=Khat,alpha=0.05,boot.thr=FALSE)
### In general, boot.thr = T works better for small sample but is slower.
### It was used in the paper.
fit$coef.mat
### notice that beta1 inference is meaningful here. Check the paper.
beta
fit$chisq.p
## find a parametric estimation of the network. This is generally not available.
rsc <- reg.SP(A,K=Khat,tau=0.1)
est <- SBM.estimate(A,rsc$cluster)
Phat <- est$Phat
fit2 <- SP.Inf(X.true,Y,Phat,K=Khat,alpha=0.05,boot.thr=FALSE)
fit2$coef.mat
### notice that beta1 inference is meaningful here. Check the paper.
Run the code above in your browser using DataLab