mydata<-mydata1[, c("ISII1", "ISII2", "ISII3","ISII4","ISII5","ISII6","ISII7")]…
- User
mydata<-mydata1[, c("ISII1", "ISII2", "ISII3","ISII4","ISII5","ISII6","ISII7")]
#根据自己的数据给每列值赋予名称 myname<-c("LT","LE","ST","SE","PMS1","PMS2","PMS3","SIS1","SIS2","SIS3","PPC1",
-
code
"PPC2","PPC3","PPC4","PSSS1","PSSS2","PSSS3","CPSS1","CPSS2")
colnames(mydata)<-myname mydata.frame<-myname
#根据每列量表属性进行分组,比如1-4列属于Dpress(这个名字随意) feature_group<-list(Dpress=c(1:4),PHQ=c(5:7),GAD=c(8:10),ISI=c(11:14),g4=c(15:17),g5=c(18:19))
#使用Lasso算法对相关矩阵进行稀疏化,调优参数设置为0.5 #网络的可视化采用Fruchterman-Reingold算法,连接强且数量多的节点出现在网络的中心附近,连接弱且数量少的节点行走在网络的外围
qgraph()中使用Fruchterman-Reingold算法,布局参数需要设置为“spring”。
R-package mgm计算predictability
Compute node predictability 体现在网络图中每个节点外的小圆圈
library(mgm) p <- ncol(mydata) mydata<-as.matrix(mydata) fit_obj <- mgm(data = mydata,
-
code
type = rep('g', p), -
code
level = rep(1, p), -
code
lambdaSel = 'CV', -
code
ruleReg = 'OR', -
code
pbar = FALSE)
Note that the sign of parameter estimates is stored separately; see ?mgm>
pred_obj <- predict(fit_obj, mydata)
Compute graph with tuning = 0.5 (EBIC)
即构建网络并使用Lasso算法对相关矩阵进行稀疏化,调优参数设置为0.5
CorMat <- cor_auto(mydata) Variables detected as ordinal: LT; LE; ST; SE; PMS1; PMS2; PMS3 EBICgraph <- qgraph(CorMat, graph = "glasso", sampleSize = nrow(mydata),groups=feature_group, nodeNames=myname,
-
code
tuning = 0.5, layout = "spring", details = TRUE, threshold = TRUE, pie = pred_obj$error[,2])
Note: Network with lowest lambda selected as best network: assumption of sparsity might be violated. Error in if (xx == "circle") { : missing value where TRUE/FALSE needed In addition: Warning message: In lcolor[is.na(lcolor)] <- ifelse(vertex.colors == "background", : number of items to replace is not a multiple of replacement length
-
- Assistant
It seems that there is an error in the code. The specific error message is "missing value where TRUE/FALSE needed" and it occurs in the line where the pie argument is assigned in the qgraph function. Additionally, there is a warning message stating that the number of items to replace is not a multiple of replacement length.
To fix this issue, you can try the following:
-
Check the input data "pred_obj$error[,2]" to ensure that it is a valid argument for the pie parameter. Make sure it has the correct length and does not contain any missing or null values.
-
Ensure that the variable "myname" contains the correct names for the nodes in the network graph. Double-check for any typos or missing names.
-
Verify that the layout parameter "spring" is a valid value. If not, try using a different layout algorithm or remove the layout argument altogether.
-
Consider trying different values for the tuning parameter (e.g., 0.1, 0.2, etc.) to see if it resolves the error.
-
Make sure that all required packages (e.g., qgraph, mgm) are installed and loaded correctly.
By resolving these issues, you should be able to generate the network graph without errors.
-
- User
可以帮我修改一下吗
- Assistant
Certainly! Here's a modified version of the code, taking into account the mentioned issues:
R# Compute node predictability library(mgm) p <- ncol(mydata) mydata <- as.matrix(mydata) fit_obj <- mgm(data = mydata, type = rep('g', p), level = rep(1, p), lambdaSel = 'CV', ruleReg = 'OR', pbar = FALSE) pred_obj <- predict(fit_obj, mydata) # Compute graph with tuning = 0.5 (EBIC) CorMat <- cor_auto(mydata) EBICgraph <- qgraph(CorMat, graph = "glasso", sampleSize = nrow(mydata), groups = feature_group, nodeNames = myname, tuning = 0.5, layout = "spring", details = TRUE, threshold = TRUE, pie = pred_obj$fits)