R
来自OBHRM百科
R是一个用于统计计算和统计制图的优秀免费工具。与SPSS、SAS等相比,R是属于GNU系统的一个自由、免费、源代码开放的软件。使用R进行统计分析,是免费用正版软件进行统计分析。
目录
R的安装与升级
- R的官方网站:https://www.r-project.org/
- R的安装:您可以从:https://cran.r-project.org/ 下载R软件。如果网络速度慢,官网提供了许多下载R软件的镜像站点,详情请访问:https://cran.r-project.org/mirrors.html ,清华大学的镜像地址为:https://mirrors.tuna.tsinghua.edu.cn/CRAN。安装过程基本上都是直接点“下一步”即可,默认安装后界面为中文,如果需要将界面调整为英语。安装R后,建议同时安装RStudio,官网下载地址:https://posit.co/download/rstudio-desktop/
- R的升级:采用installr包;手工升级。
- R包的安装目录
- R的引用
R基本函数
在r控制台,输入:builtins(),就可以得到r语言内置的基本函数清单。
sessionInfo()
Sys.setlocale(category="LC_ALL",locale="chinese")
"LC_COLLATE=Chinese (Simplified)_China.936;LC_CTYPE=Chinese (Simplified)_China.936;LC_MONETARY=Chinese (Simplified)_China.936;LC_NUMERIC=C;LC_TIME=Chinese (Simplified)_China.936"
R常用包
- R包列表:https://cran.r-project.org/web/packages/available_packages_by_name.html
- R常用包
- R中介效应分析包,包括Moderated mediation(包名:mediation):https://cran.r-project.org/web/packages/mediation/index.html
- R调节回归分析包(包名:pequod):https://cran.r-project.org/web/packages/pequod/index.html
- 调节的中介分析包(包名:manymome):https://cran.r-project.org/web/packages/manymome/
- 与SPSS PROCESS宏对应的processR包(包名:processR):https://cran.r-project.org/web/packages/processR/index.html ,已经下架
- R响应面分析(Response Surface Analysis)包(包名:RSA):https://cran.r-project.org/web/packages/RSA/index.html
- R结构方程包(包名:sem):https://cran.r-project.org/web/packages/sem/index.html
- R结构方程包(包名:lavaan):https://cran.r-project.org/web/packages/lavaan/index.html
- R结构方程包(包名:OpenMx):https://cran.r-project.org/web/packages/OpenMx/index.html
- R结构方程工具包(包名:semTools):https://cran.r-project.org/web/packages/semTools/index.html
- R结构方程画图(包名:semptools.html):https://cran.r-project.org/web/packages/semptools/
- R Multilevel分析包(包名:multilevel):https://cran.r-project.org/web/packages/multilevel/index.html ,这个包里头还包括AMJ、JAP等文章的数据与分析程序。
- R Multilevel分析包(包名:lme4):https://cran.r-project.org/web/packages/lme4/index.html
- R Multilevel分析(包名:nlme):https://cran.r-project.org/web/packages/nlme/index.html
- R元分析包(包名:metafor):https://cran.r-project.org/web/packages/metafor/index.html
- R菜单式元分析包(包名:MAVIS):https://cran.r-project.org/web/packages/MAVIS/index.html
- R菜单式元分析包(包名:RcmdrPlugin.MA):https://cran.r-project.org/web/packages/RcmdrPlugin.MA/index.html
- R统计效应分析(Power Analysis)包(包名:pwr):https://cran.r-project.org/web/packages/pwr/index.html
- R心理统计包(包名:psych):http://personality-project.org/r/r.guide.html#psych
- R图形与表格辅助工具(包名:sjPlot):https://cran.r-project.org/web/packages/sjPlot/index.html
- R菜单式基本统计包(包名:Rcmdr):https://cran.r-project.org/web/packages/Rcmdr/index.html
- R菜单式数据挖掘包(包名:rattle):https://cran.r-project.org/web/packages/rattle/
- R文献分析包(包名:bibliometrix):https://cran.r-project.org/web/packages/bibliometrix/index.html
- R自动生成APA格式表格的的包1(包名:apa):https://cran.r-project.org/web/packages/apa/index.html
- R自动生成APA格式表格的的包2(包名:apaTables):https://cran.r-project.org/web/packages/apaTables/index.html
- R Task View:https://cran.r-project.org/web/views/
R包的安装与卸载
- 远程安装:在R的控制台,输入install.packages("包的名称")。比如,安装lavaan,就是install.packages("lavaan")。正常情况下,安装会自动完成。如果网络不正常,可能会出现错误,建议改用“本地安装”
- 本地安装:从官方网站下载统计软件包,然后安装:R菜单—程序包—本地安装/install package(s) from local files...。当网络速度不佳时,可以考虑这种安装方式。
- 从Github安装:部分最新的版本,作者暂时未上传到cran,可以从Github直接安装。
- R包版本信息:packageVersion("包的名称")。比如,需要知道现在安装是哪个版本的lavaan,请输入:packageVersion("lavaan")
- R包的更新:如果要更新所有的包,并询问是否更新,直接输入:update.packages();如果希望不询问,自动更新所有包,请输入:update.packages(ask=F);如果只是更新某个特定的包,等同于重新安装该包,请输入:install.packages()
- 包的卸载: remove.packages("包的名称")。比如,卸载lavaan,就是remove.packages("lavaan")。
R:数据读取与管理
1、设定工作目录
- 查看当前工作目录,在R控制台输入:getwd()
- 设置工作目录:R菜单-文件-改变工作目录,再选择对应的工作目录就可以。或者在R控制台输入:curwd=setwd("路径"),但是路径中的\要全部替换成\\,比如:curwd=setwd("c:\\program files\\mplus\\mplus examples\\user's guide examples")
2、读取数据
- 从文本文件中读取固定格式数据:如果您的数据还没有输入计算机,建议直接以txt格式输入数据。不需要编制任何软件,可以用Windows系统自带的Edit,或者Editplus、Notepad++输入数据。输入完成后,直接用固定格式读取数据。
- 从文本文件中读取自由格式数据
- 读取CSV格式的数据
- 读取Excel数据
- 读取SPSS、SAS、Stata等的数据文件
4、数据转换: 变量重命名 变量重编码,比如反向计分题目的调整
8、保存数据: 保存为CSV文件 保存为文本文件 保存为Excel文件 保存为其他统计软件文件
OBHRM研究常用统计分析:R的应用
1、 信度分析
2、探索性因素分析:不旋转(如,Harman检验) 正交旋转 斜交旋转
3、验证性因素分析: 一阶验证性因素分析 二阶验证性因素分析 BiFactor验证性因素分析
5、 相关分析
8、路径分析: 中介变量的路径分析 有调节的中介路径分析
9、结构方程模型: 结构方程模型
10、面板数据(Panel Data): 面板数据分析
RMarkdown
https://github.com/crsh/papaja
https://github.com/methexp/pdl2/blob/master/main.rmd
https://rmarkdown.rstudio.com/articles_docx.html
R推荐图书与资源
- 《Learning Statistics with R》,PDF版免费下载:https://learningstatisticswithr.com/ ,同时可以免费下载该书中用过的数据,以及对应R包:lsr(https://cran.r-project.org/web/packages/lsr/index.html )
- 《R实战》(R in Action 中译本),人民邮电出版社,Robert I. Kabacoff 著,高涛,肖楠,陈钢译,2013年
- R心理测量包大全:https://cran.r-project.org/web/views/Psychometrics.html
- 提供大量各类统计的R教程:https://quantdev.ssri.psu.edu/tutorials
- 数据:stevedata
- https://www.math.pku.edu.cn/teachers/lidf/docs/Rbook/html/_Rbook/bookdown-template-v0-6.zip
R FAQs
- 如何引用R语言,或某个具体的包
- 如何查询R包的下载量
- library(help = "base")
- 如何修改Rstudio的console的显示语言为英文
- package类型:getOption("pkgType") options(pkgType="source") options(pkgType="binary")