#--------------------------------------------#
# 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. 04 Spring:01.Spring框架简介&&02.程序间耦合&&03.Spring的 IOC 和 DI&&08.面向切面编程 AOP&&10.Spring中事务控制

    spring共四天 第一天:spring框架的概述以及spring中基于XML的IOC配置 第二天:spring中基于注解的IOC和ioc的案例 第三天:spring中的aop和基于XML以及注解的A ...

  2. win10下安装cygwin全过程

    简单讲:cygwin就是在windows系统上跑linux和unix的环境,跨平台移植的应用程序移植. 安装步骤: 下载cygwin: 打开官网https://cygwin.com/install.h ...

  3. 帝国CMS7.5后台美化模板 后台风格修改 帝国CMS后台模板

    都知道帝国CMS功能强悍,生成静态html也非常好用.可是有时候他的后台样式,丑的让你不想用,dede呢,漏洞太多,PHPCMS好看,可是门槛要求高,你会写PHP才行. 帝国CMS后台美化模板:全面美 ...

  4. Mysql存储过程案例集合

    注:使用的工具为SQLyog 壹.while简单使用(替换字符串中的字符,和REPLACE效果一样) 注: 这里没有使用REPLACE函数 1.创建存储过程 DROP PROCEDURE IF EXI ...

  5. Ubuntu16装Flash

    第一种方法: 1)下载flash的tar.gz压缩包.(以·tar.gz为后缀的文件是一种压缩文件,在Linux和macOS下常见,Linux和macOS都可以直接解压使用这种压缩文件) https: ...

  6. 记录一次追踪@AutoWired的过程

    目录 记录一次追踪@AutoWired的过程 前言 疑惑:依赖究竟是怎么自动注入的 AutoWiredAnnotationBeanPostProcessor中探究 自动注入debug流程追踪 dete ...

  7. CodeForces 990D Graph And Its Complement(图和补图、构造)

    http://codeforces.com/problemset/problem/990/D 题意: 构造一张n阶简单无向图G,使得其连通分支个数为a,且其补图的连通分支个数为b. 题解: 第一眼看到 ...

  8. 17.3.16---python内建函数

    内置函数,无需import,任何时候都可以直接被使用 1------ Python针对众多的类型,提供了众多的内建函数来处理(内建是相对于导入import来说的,后面学习到包package时,将会介绍 ...

  9. 画图认识--matplotlib.pyplot

    matplotlib的pyplot模块提供了和MATLAB类似的绘图API,方便用户快速绘制二维图表.我们先看一个简单的 import matplotlib.pyplot as plt import ...

  10. yum安装与卸载软件常见命令

    1.使用yum安装和卸载软件,有个前提是yum安装的软件包都是rpm格式的. 安装的命令是,yuminstall ~,yum会查询数据库,有无这一软件包,如果有,则检查其依赖冲突关系,如果没有依赖冲突 ...