2017-04-15 67 views
0

我跟隨THIS R Blogger教程計算Cronbach alpha,它完美地工作。我想了解如何將結果導出到data.frame或文本文件中。任何想法我可能能夠從以下代碼中導出結果:psych::alpha(d)?請注意,我查看了stargazer軟件包,但無法使用Cronbach輸出迴歸和描述性統計工作。謝謝。導出Cronbach Alpha結果 - R

+0

,這是讓控制檯輸出的文本文件轉儲是使用'capture.output'或'sink' 。 (之前很多人問過我,所以我不想重複一個現有的答案。)如果這些功能沒有提供一個成功的途徑,那麼請閱讀[MCVE]。 (無論如何,鏈接到博客並不被認爲是編碼方面的充分努力,IMO。) –

回答

1

輸出可以這樣保存爲一個txt文件。您還可以使用$操作者只得到你感興趣的信息子集的alpha功能創建的對象。

setwd("~/Desktop") 
out <- psych::alpha(d) 
capture.output(out,file = "alpha.txt") 
0

正如一切的R真實的,有你想要做什麼做的很多方法。首先要看功能的幫助菜單(在這種情況下?alpha)。在那裏你會看到許多對象是從alpha函數返回的。 (這是幫助文件的「值」部分中列出的內容)。

當您輸出alpha時,只顯示這些對象的子集。然而,看到返回的對象的完整列表,請使用「STR」命令

my.results <- alpha(my.data) 
str(my.results)  #or just list the names of the objects 
names(my.alpha) 
[1] "total"   "alpha.drop" "item.stats" "response.freq" "keys"   "scores"  "nvar"   "boot.ci"  
[9] "boot"   "Unidim"  "Fit"   "call"   "title" 

然後,您可以選擇捕捉到任何對象供自己使用。 因此

my.alpha <- alpha(ability) #use the ability data set in the psych package 
my.alpha #will give the normal (and nicely formatted output) 
totals <- my.alpha$total #just get one object from my.alpha 
totals      #show that object 

會產生一個單一的線(沒有花俏的輸出):

raw_alpha std.alpha G6(smc) average_r  S/N   ase  mean  sd 
0.8292414 0.8307712 0.8355999 0.2347851 4.909159 0.006384736 0.5125148 0.2497765 

可以爲任何對象返回做到這一點。我們大多數人寫封裝打印我們認爲是功能輸出的基本要素,但包括其他有用的信息。我們還允許其他功能(例如摘要)打印出其他信息。

因此,使用從上面的例子中,

summary(my.alpha) #prints the rounded to 2 decimals my.alpha$total object 
Reliability analysis 
raw_alpha std.alpha G6(smc) average_r S/N ase mean sd 
     0.83  0.83 0.84  0.23 4.9 0.0064 0.51 0.25 

謹慎的最後一個字。我們中的許多人沒有發現α是描述量表結構的特別有用的統計量。你可能想通常的做法閱讀有關如何使用迷幻包找到係數歐米茄教程在

http://personality-project.org/r/psych/HowTo/R_for_omega.pdf