1. #--------------------------------------------#
  2. # R in Action (2nd ed): Chapter 14 #
  3. # Principal components and factor analysis #
  4. # requires package psych #
  5. # install.packages("psych") #
  6. #--------------------------------------------#
  7.  
  8. par(ask=TRUE)
  9. set.seed(1234) # make results reproducible
  10.  
  11. # Listing 14.1 - Principal components analysis of US Judge Ratings
  12. library(psych)
  13. pc <- principal(USJudgeRatings[,-1], nfactors=1)
  14. pc
  15.  
  16. # Principal components analysis Harman23.cor data
  17. library(psych)
  18. fa.parallel(Harman23.cor$cov, n.obs=302, fa="pc", n.iter=100,
  19. show.legend=FALSE, main="Scree plot with parallel analysis")
  20.  
  21. # Listing 14.2 - Principal components analysis of body measurements
  22. library(psych)
  23. PC <- principal(Harman23.cor$cov, nfactors=2, rotate="none")
  24. PC
  25.  
  26. # Listing 14.3 - Principal components analysis with varimax rotation
  27. rc <- principal(Harman23.cor$cov, nfactors=2, rotate="varimax")
  28. rc
  29.  
  30. # Listing 14.4 - Obtaining componenet scores from raw data
  31. library(psych)
  32. pc <- principal(USJudgeRatings[,-1], nfactors=1, score=TRUE)
  33. head(pc$scores)
  34. cor(USJudgeRatings$CONT, pc$score)
  35.  
  36. # Listing 14.5 - Obtaining principal component scoring coefficients
  37. library(psych)
  38. rc <- principal(Harman23.cor$cov, nfactors=2, rotate="varimax")
  39. round(unclass(rc$weights), 2)
  40.  
  41. ## Exploratory factor analysis of ability.cov data
  42.  
  43. options(digits=2)
  44. library(psych)
  45. covariances <- ability.cov$cov
  46. # convert covariances to correlations
  47. correlations <- cov2cor(covariances)
  48. correlations
  49.  
  50. # determine number of factors to extract
  51. fa.parallel(correlations, n.obs=112, fa="both", n.iter=100,
  52. main="Scree plots with parallel analysis")
  53.  
  54. # Listing 14.6 - Principal axis factoring without rotation
  55. fa <- fa(correlations, nfactors=2, rotate="none", fm="pa")
  56. fa
  57.  
  58. # Listing 14.7 - Factor extraction with orthogonal rotation
  59. fa.varimax <- fa(correlations, nfactors=2, rotate="varimax", fm="pa")
  60. fa.varimax
  61.  
  62. # Listing 14.8 - Factor extraction with oblique rotation
  63. fa.promax <- fa(correlations, nfactors=2, rotate="promax", fm="pa")
  64. fa.promax
  65.  
  66. # calculate factor loading matrix
  67. fsm <- function(oblique) {
  68. if (class(oblique)[2]=="fa" & is.null(oblique$Phi)) {
  69. warning("Object doesn't look like oblique EFA")
  70. } else {
  71. P <- unclass(oblique$loading)
  72. F <- P %*% oblique$Phi
  73. colnames(F) <- c("PA1", "PA2")
  74. return(F)
  75. }
  76. }
  77. fsm(fa.promax)
  78.  
  79. # plot factor solution
  80. factor.plot(fa.promax, labels=rownames(fa.promax$loadings))
  81. fa.diagram(fa.promax, simple=FALSE)
  82.  
  83. # factor scores
  84. 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. 吴裕雄--天生自然 JAVA开发学习:正则表达式

    import java.util.regex.*; class RegexExample1{ public static void main(String args[]){ String conten ...

  2. Ubuntu 安装软件时显示:无法获得锁 /var/lib/dpkg/lock -open(资源暂时不可用)

    出错状况:在用 sudo apt-get install 安装软件时,结果终端提示: 无法获得锁 /var/lib/dpkg/lock -open(资源暂时不可用) 无法锁定管理目录(var/lib/ ...

  3. StdinNotImplementedError: raw_input was called, but this frontend does not support input requests.

    当时VS CODE内嵌的jupyter 交互界面的时候,出现了这个错误 原因是,这样的界面不支持行输入.可以使用cmd终端或其他方式运行该文件进行交互输入

  4. PowerShell创建 Profile

    profile主要用于个性化常用的函数.别名等等.每次加载powershell的时候,都会执行profile中的内容. 查看是否有profile: $profile 如果结果是false说明没有.则创 ...

  5. JavaEE--分布式对象

    参考:http://blog.csdn.net/smcwwh/article/details/7080997 1.客户与服务器的角色 所有分布式编程技术的基本思想都很简单:客户计算机产生一个请求,然后 ...

  6. LCA 最近公共祖先 Tarjan(离线)算法的基本思路及其算法实现

    首先是最近公共祖先的概念(什么是最近公共祖先?): 在一棵没有环的树上,每个节点肯定有其父亲节点和祖先节点,而最近公共祖先,就是两个节点在这棵树上深度最大的公共的祖先节点. 换句话说,就是两个点在这棵 ...

  7. mysql引擎与物理文件

    SELECT VERSION();show GLOBAL VARIABLES like '%PARTITION%';-- 查看分区情况 show GLOBAL VARIABLES like '%dat ...

  8. C++ malloc()函数的注意点及使用示例

    1.malloc()函数的头文件是stdlib.h,其函数声明如下: void* malloc(size_t size); 其中参数size_t size表示动态内存分配空间的大小,以字节为单位. s ...

  9. xcode6 Images.xcassets添加LaunchImage

    不使用LaunchScreen.xib,通过Images.xcassets新建LaunchImage做登陆界面 步骤: 1.创建LaunchImage

  10. mysql SQL优化琐记之索引

    equal最好了,其次in,最后是range !=  <>  这类非操作尽量不用,它会转换为range.>都是范围查询 复合索引有左匹配原则,(clo_a,clo_b)相当建立了两个 ...