【问题标题】:R simulation function based on psych package not saving values基于心理包的R模拟功能不保存值
【发布时间】:2016-02-22 15:34:03
【问题描述】:

我正在尝试为R 编写一个使用两个包的函数:simsempsych。简而言之,我希望该函数允许我使用simsem 指定要绘制的每个随机样本的大小,使用psych 指定要在每个样本上运行的因子分析选项(fa),以及然后将每个样本的fa 模型中的值保存在数据框中,以便我以后可以在它们之间进行汇总。

但是,该功能目前不工作;没有任何值从函数的任何重复中保存在目标数据帧中。

下面的可重现示例和我的功能代码。非常感谢您对出了什么问题的任何见解。

#Install and call simsem and psych package, to simulate datasets based on population model, and fit fa model
install.packages("simsem")
library(simsem)

install.packages("psych")
library(psych)

#Specify Population Model
popModel<-"
f1 =~ 0.7*y1 + 0.7*y2 + 0.7*y3
f2 =~ 0.5*y4 + 0.5*y5 + 0.5*y6
f1 ~~ 1*f1
f2 ~~ 1*f2
f1 ~~ 0.50*f2
y1 ~~ 0.51*y1
y2 ~~ 0.51*y2
y3 ~~ 0.51*y3
y4 ~~ 0.75*y4
y5 ~~ 0.75*y5
y6 ~~ 0.75*y6
"

#Function: User specifies number of reps, sample size, number of factors, rotation and extraction method
#Then for each rep, a random sample of popModel is drawn, a FA model is fit, and two logical values are saved
#in data.frame fit
sample.efa = function(rep, N, nfac, rotation, extract){
 fit = data.frame(RMSEA= logical(0), TLI = logical(0)) #create empty data frame with two columns
 for(i in seq_len(rep)){
    dat = generate(popModel, N) #draw a random sample of N size from popModel
    model = fa(dat, nfactors = nfac, rotate = rotation, fm = extract, SMC = TRUE, alpha = .05) #fit FA model with user specified options from function
    store = data.frame(RMSEA=all(model$rms < .08), TLI = all(model$TLI > .90)) #save two logical values in "store"
names(store) = names(fit) #give "store" same names as target data-frame
    fit=rbind(fit, store) #save values from each iteration of "store" in target data-frame "fit"
  }
}

#Run test of function with small number of reps (5) of sample sizes of 200
set.seed(123)
sample.efa(5, N = 200, nfac = 2, rotation = "promax", extract = "ml")

【问题讨论】:

  • for(i in rep)...也许可以试试for(i in seq_len(rep))
  • 抱歉,修复了 popModel 代码中的一个错误。另外,尝试了您的建议@cory,我收到了一堆警告,其效果是“在[&lt;-.factor(*tmp*, ri, value = "X.RMSEA.") :无效因子水平,NA 生成”,并且没有数据框已生成。

标签: r function psych


【解决方案1】:

您错误地更改了数据框的名称...

sample.efa = function(rep, N, nfac, rotation, extract){
  fit = data.frame(RMSEA= logical(0), TLI = logical(0)) #create empty data frame with two columns
  for(i in seq_len(rep)){
    dat = generate(popModel, N) #draw a random sample of N size from popModel
    model = fa(dat, nfactors = nfac, rotate = rotation, fm = extract, SMC = TRUE, alpha = .05) #fit FA model with user specified options from function
    store = data.frame(RMSEA=all(model$rms < .08), TLI = all(model$TLI > .90)) #save two logical values in "store"
    names(store) = names(fit) #give "store" same names as target data-frame
    fit=rbind(fit, store) #save values from each iteration of "store" in target data-frame "fit"
  }
  return(fit)
}

另外,您可能希望将stringsAsFactors=FALSE 添加到您的数据帧以避免以后出现问题...

【讨论】:

  • 这消除了我从你的代码中得到的警告,但我仍然没有得到我想要的创建的数据框(我会用你的修改来更新我的原始帖子)
  • 1.这不是我的代码……它是你的。不要怪我。 2. 你忘了从函数中返回你的数据框。我在上面的回复中编辑了您的代码。
  • 我很抱歉——我并不想表现得像是在责备你。恰恰相反;我在这里发帖是因为编程对我来说是一个挑战。 return(fit) 行解决了我遇到的问题,谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-02-20
  • 2020-06-19
  • 2018-03-27
  • 1970-01-01
  • 2016-08-17
  • 1970-01-01
  • 2015-11-27
相关资源
最近更新 更多