#--------------------------------------------#
# R in Action (2nd ed): Chapter 14 #
# Principal components and factor analysis #
# requires package psych #
# install.packages("psych") #
#--------------------------------------------# par(ask=TRUE)
set.seed(1234) # make results reproducible # Listing 14.1 - Principal components analysis of US Judge Ratings
library(psych)
pc <- principal(USJudgeRatings[,-1], nfactors=1)
pc # Principal components analysis Harman23.cor data
library(psych)
fa.parallel(Harman23.cor$cov, n.obs=302, fa="pc", n.iter=100,
show.legend=FALSE, main="Scree plot with parallel analysis") # Listing 14.2 - Principal components analysis of body measurements
library(psych)
PC <- principal(Harman23.cor$cov, nfactors=2, rotate="none")
PC # Listing 14.3 - Principal components analysis with varimax rotation
rc <- principal(Harman23.cor$cov, nfactors=2, rotate="varimax")
rc # Listing 14.4 - Obtaining componenet scores from raw data
library(psych)
pc <- principal(USJudgeRatings[,-1], nfactors=1, score=TRUE)
head(pc$scores)
cor(USJudgeRatings$CONT, pc$score) # Listing 14.5 - Obtaining principal component scoring coefficients
library(psych)
rc <- principal(Harman23.cor$cov, nfactors=2, rotate="varimax")
round(unclass(rc$weights), 2) ## Exploratory factor analysis of ability.cov data options(digits=2)
library(psych)
covariances <- ability.cov$cov
# convert covariances to correlations
correlations <- cov2cor(covariances)
correlations # determine number of factors to extract
fa.parallel(correlations, n.obs=112, fa="both", n.iter=100,
main="Scree plots with parallel analysis") # Listing 14.6 - Principal axis factoring without rotation
fa <- fa(correlations, nfactors=2, rotate="none", fm="pa")
fa # Listing 14.7 - Factor extraction with orthogonal rotation
fa.varimax <- fa(correlations, nfactors=2, rotate="varimax", fm="pa")
fa.varimax # Listing 14.8 - Factor extraction with oblique rotation
fa.promax <- fa(correlations, nfactors=2, rotate="promax", fm="pa")
fa.promax # calculate factor loading matrix
fsm <- function(oblique) {
if (class(oblique)[2]=="fa" & is.null(oblique$Phi)) {
warning("Object doesn't look like oblique EFA")
} else {
P <- unclass(oblique$loading)
F <- P %*% oblique$Phi
colnames(F) <- c("PA1", "PA2")
return(F)
}
}
fsm(fa.promax) # plot factor solution
factor.plot(fa.promax, labels=rownames(fa.promax$loadings))
fa.diagram(fa.promax, simple=FALSE) # factor scores
fa.promax$weights

吴裕雄--天生自然 R语言开发学习:主成分分析和因子分析(续一)的更多相关文章

  1. 吴裕雄--天生自然 R语言开发学习:聚类分析(续一)

    #-------------------------------------------------------# # R in Action (2nd ed): Chapter 16 # # Clu ...

  2. 吴裕雄--天生自然 R语言开发学习:时间序列(续三)

    #-----------------------------------------# # R in Action (2nd ed): Chapter 15 # # Time series # # r ...

  3. 吴裕雄--天生自然 R语言开发学习:时间序列(续二)

    #-----------------------------------------# # R in Action (2nd ed): Chapter 15 # # Time series # # r ...

  4. 吴裕雄--天生自然 R语言开发学习:时间序列(续一)

    #-----------------------------------------# # R in Action (2nd ed): Chapter 15 # # Time series # # r ...

  5. 吴裕雄--天生自然 R语言开发学习:方差分析(续二)

    #-------------------------------------------------------------------# # R in Action (2nd ed): Chapte ...

  6. 吴裕雄--天生自然 R语言开发学习:方差分析(续一)

    #-------------------------------------------------------------------# # R in Action (2nd ed): Chapte ...

  7. 吴裕雄--天生自然 R语言开发学习:回归(续四)

    #------------------------------------------------------------# # R in Action (2nd ed): Chapter 8 # # ...

  8. 吴裕雄--天生自然 R语言开发学习:回归(续三)

    #------------------------------------------------------------# # R in Action (2nd ed): Chapter 8 # # ...

  9. 吴裕雄--天生自然 R语言开发学习:回归(续二)

    #------------------------------------------------------------# # R in Action (2nd ed): Chapter 8 # # ...

  10. 吴裕雄--天生自然 R语言开发学习:回归(续一)

    #------------------------------------------------------------# # R in Action (2nd ed): Chapter 8 # # ...

随机推荐

  1. 记一次asp.net core 线上崩溃解决总结

    1.首先要先准备好环境,安装lldb 工具 要安装3.9版本的,因为每个版本对应dnc版本不一样,3.9的支持2.2 版本,然后确定分析的机器里dnc 版本和线上的生产环境是否一致,自己安装比较费劲, ...

  2. 修改自己的centos输入法

    当自己的centos连上网时,就可以修改自己的输入法了 http://jingyan.baidu.com/album/da1091fb3e7f8a027849d681.html?picindex=2

  3. day59-mysql-存储过程、函数、事务、锁、备份

    存储过程.函数不是重要的内容. 三. 存储过程:类似于函数(方法),简单的说存储过程是为了完成某个数据库中的特定功能而编写的语句集合, 该语句集包括SQL语句(对数据的增删改查).条件语句和循环语句等 ...

  4. [HNOI2019]白兔之舞(矩阵快速幂+单位根反演)

    非常抱歉,这篇文章鸽了.真的没时间写了. #include<bits/stdc++.h> using namespace std; typedef long long ll; #defin ...

  5. ORM表之间高级设计

    ORM表之间高级设计 一.表的继承 # db_test1 # 一.基表 # Model类的内部配置Meta类要设置abstract=True, # 这样的Model类就是用来作为基表 # 多表:Boo ...

  6. 5.docker image (镜像)

    1.image 是什么 是文件和 meta data 的集合 (root filesystem) 是分层的,并且每一层都可以添加改变删除文件,成为一个新的image 不同的image可以共享相同的la ...

  7. Proe 导出PDF Vb.net

    Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSt ...

  8. dubbo通信协议对比

    对dubbo的协议的学习,可以知道目前主流RPC通信大概是什么情况,本文参考dubbo官方文档 http://dubbo.io/User+Guide-zh.htm dubbo共支持如下几种通信协议: ...

  9. 解决 WinForm 重写 CreateParams 隐藏窗口以后的显示问题

    WinForm 启动时隐藏窗体最简单有效的办法是重写 CreateParams protected override CreateParams CreateParams { get { base.Vi ...

  10. 在维护项目中的UUID工具类

    import java.util.UUID; /** * <p> * Title:uuID生成器 * </p> * <p> * Description:UUID 标 ...