0

とのR2 つのパッケージを使用する関数を作成しようとしています。簡単に言えば、 を使用して抽出する各ランダム サンプルのサイズを指定し、 を使用して各サンプルで実行する因子分析 ( ) のオプションを指定し、各モデルの値を保存できる関数が必要です。データフレームでサンプルして、後でそれらを集計できるようにします。simsempsychsimsemfapsychfa

ただし、この機能は現在機能していません。関数の繰り返しから、ターゲットデータフレームに値が保存されていません。

以下の再現可能な例と私の関数コード。何がうまくいかないのかについての洞察に本当に感謝します。

#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")
4

1 に答える 1

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後で問題を回避するために、データフレームに追加することもできます...

于 2016-02-22T16:43:55.400 に答える