因子分析的一个小例子

2023-10-31 10:50
文章标签 例子 因子分析

本文主要是介绍因子分析的一个小例子,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

这是学习笔记的第 1997 篇文章


  今天做了下因子分析中的东东,本来想找一些公共网站的数据,限于时间和要做一些数据整理,时间来不及,就找了一个现成的数据源。 

这是洛杉矶等十二个大都市的人口调查获得的,包含了5个社会以经济变量:人口总数,居民受教育年限,佣人总数,服务行业人数,中等的房价。

为了方便我把数据集先提供出来。

人口(X1)教育年限(X2)佣人数(X3)服务人数(X4)房价(X5)
570012.8250027025000
100010.96001010000
34008.81000109000
380013.6170014025000
400012.8160014025000
82008.326006012000
120011.44001016000
910011.533006014000
990012.5340018018000
960013.7360039025000
96009.633008012000
940011.4400010013000

 我们把数据存储在excel里面,然后使用R语言来做分析。

首先导入数据,如果程序包openxlsx没有的话,就在R语言里安装下依赖。假设文件的路径是   D:\\yinzifenxi.xlsx

library(openxlsx)

读取excel的数据

data1 <- read.xlsx("D:\\yinzifenxi.xlsx" )

输出部分信息

> head(data1)

  人口(X1) 教育(X2) 佣人(X3) 服务(X4) 房价(X5)

1     5700     12.8     2500      270    25000

2     1000     10.9      600       10    10000

3     3400      8.8     1000       10     9000

4     3800     13.6     1700      140    25000

5     4000     12.8     1600      140    25000

6     8200      8.3     2600       60    12000

> data1_cor <- cor(data1)

> head(cor(data1),5)

         

> head(cor(data1),5)

           人口(X1)   教育(X2)  佣人(X3)  服务(X4)   房价(X5)

人口(X1) 1.00000000 0.00975059 0.9724483 0.4388708 0.02241157

教育(X2) 0.00975059 1.00000000 0.1542838 0.6914082 0.86307009

佣人(X3) 0.97244826 0.15428378 1.0000000 0.5147184 0.12192599

服务(X4) 0.43887083 0.69140824 0.5147184 1.0000000 0.77765425

房价(X5) 0.02241157 0.86307009 0.1219260 0.7776543 1.00000000

> library(psych)

确定因子数量

> fa.parallel(data1_cor, n.obs = 112, fa = "both", n.iter = 100)

Parallel analysis suggests that the number of factors =  2  and the number of components =  2 

There were 18 warnings (use warnings() to see them)

得到的碎石图如下:

640?wx_fmt=png

从这样的数据分析可以看到前2个会占据主要的部分,保留2个主成分即可。

接下来要做因子分析了,第一个参数是数据,第二个参数说明要保留两个主成分,第三个参数为旋转方法,为none,先不进行主成分旋转,第四个参数表示提取公因子的方法为最大似然法,不是机器学习的意思。 

> fa_model1 <- fa(data1_cor, nfactors = 2, rotate = "none", fm = "ml")

输出分析的结果内容:

> fa_model1

Factor Analysis using method =  ml

Call: fa(r = data1_cor, nfactors = 2, rotate = "none", fm = "ml")

Standardized loadings (pattern matrix) based upon correlation matrix

           ML2  ML1   h2    u2 com

人口(X1) -0.03 1.00 1.00 0.005 1.0

教育(X2)  0.90 0.04 0.81 0.193 1.0

佣人(X3)  0.09 0.98 0.96 0.036 1.0

服务(X4)  0.78 0.46 0.81 0.185 1.6

房价(X5)  0.96 0.05 0.93 0.074 1.0

                       ML2  ML1

SS loadings           2.34 2.16

Proportion Var        0.47 0.43

Cumulative Var        0.47 0.90

Proportion Explained  0.52 0.48

Cumulative Proportion 0.52 1.00

Mean item complexity =  1.1

Test of the hypothesis that 2 factors are sufficient.

The degrees of freedom for the null model are  10  and the objective function was  6.38

The degrees of freedom for the model are 1  and the objective function was  0.31 

The root mean square of the residuals (RMSR) is  0.01 

The df corrected root mean square of the residuals is  0.05 

Fit based upon off diagonal values = 1

Measures of factor score adequacy             

                                                   ML2  ML1

Correlation of (regression) scores with factors   0.98 1.00

Multiple R square of scores with factors          0.95 1.00

Minimum correlation of possible factor scores     0.91 0.99

为了减少误差,需要做因子旋转,这里使用的是正交旋转法,

> fa_model2 <- fa(data1_cor, nfactors = 2, rotate = "varimax", fm = "ml")

分析结果如下:

> fa_model2

Factor Analysis using method =  ml

Call: fa(r = data1_cor, nfactors = 2, rotate = "varimax", fm = "ml")

Standardized loadings (pattern matrix) based upon correlation matrix

          ML2  ML1   h2    u2 com

人口(X1) 0.02 1.00 1.00 0.005 1.0

教育(X2) 0.90 0.00 0.81 0.193 1.0

佣人(X3) 0.14 0.97 0.96 0.036 1.0

服务(X4) 0.80 0.42 0.81 0.185 1.5

房价(X5) 0.96 0.00 0.93 0.074 1.0

                       ML2  ML1

SS loadings           2.39 2.12

Proportion Var        0.48 0.42

Cumulative Var        0.48 0.90

Proportion Explained  0.53 0.47

Cumulative Proportion 0.53 1.00

Mean item complexity =  1.1

Test of the hypothesis that 2 factors are sufficient.

The degrees of freedom for the null model are  10  and the objective function was  6.38

The degrees of freedom for the model are 1  and the objective function was  0.31 

The root mean square of the residuals (RMSR) is  0.01 

The df corrected root mean square of the residuals is  0.05 

Fit based upon off diagonal values = 1

Measures of factor score adequacy             

                                                   ML2  ML1

Correlation of (regression) scores with factors   0.98 1.00

Multiple R square of scores with factors          0.95 1.00

Minimum correlation of possible factor scores     0.91 0.99

可以看到方差比例不变,但在各观测值上的载荷发生了改变

使用factor.plot函数对旋转结果进行可视化:

> factor.plot(fa_model2)

640?wx_fmt=png

继续渲染,得到一个较为清晰的列表

> fa.diagram(fa_model2, simple = FALSE)

640?wx_fmt=png

到了这里,我们可以看到,因子1和房价,教育年限和服务人口数相关,可以抽象为经济发展因子,而因子2和人口数,佣人数相关,我们可以抽象成人口规模因子。

以上仅供参考。 

640?

这篇关于因子分析的一个小例子的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/314840

相关文章

[vivado]例子中的glbl文件

答疑帖:https://www.xilinx.com/support/answers/6537.html

工厂方法模式--结合具体例子学习工厂方法模式

在学习工厂方法模式之前,可以先学习一下简单工厂模式,网址是http://blog.csdn.net/u012116457/article/details/21650421,这里仍以水果的实例讲解。   先来说说简单工厂模式的特点,简单工厂模式将具体类的创建交给了工厂,使客户端不再直接依赖于产品,但是其违背了OCP原则,当对系统进行扩展时,仍然需要去修改原有的工厂类的代码。 而工厂方法模式则解

JS奇偶数判断例子

JS奇偶数判断例子

利用一个正则表达式的一个例子来讲解Java中的两种for循环。

package 正则表达式;public class Rudolph {public static void main(String[] args) {// TODO Auto-generated method stubString[] str = new String[]{"Rudolph","[rR]udolph","[rR][aeiou][a-z]ol.*","R.*"};for(Strin

java:spring-security的简单例子

【pom.xml】 <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId><version>2.3.12.RELEASE</version></dependency><dependency><groupId>org.springframewo

【python例子】 python 计算1000以内的水仙花数

如果一个 3 位数等于其各位数字的立方和,则称这个数为水仙花数。   例如:153 = 1^3 + 5^3 + 3^3,因此 153 就是一个水仙花数 求1000以内的水仙花数(3位数)  网上这样的例子很多,总结3种常用的方法: 代码实现: #coding:utf-8#5**3 B表示5的3次方#整除后取余数 1001%100# 方法一:for bai in range(1,1

编写android程序调用jni本地方法的实现(详细例子)

在写android程序的时候会用到jni,接下来的代码讲诉C实现,环境配置请看我其他的博客,不多说,直接上代码,代码上几乎每一句都会解释,绝对易懂 MainActivity中的代码: package com.ndk.test;import java.io.UnsupportedEncodingException;import com.example.testndk.R;import an

【Rust日报】 2019-05-30:使用最新的 async/await 的一些例子

一个说明借用/Move细节的鲜活例子 如下面例子,直接传一个元组进去,不行。索引里面的元素,直接传,可以。为什么? fn f(input: (usize, &mut usize)) {unimplemented!()}fn call_f_a_bunch_of_times(input: (usize, &mut usize)) {for _ in 0..10 {// f(input); //

使用faiss存储HuggingFaceBgeEmbeddings向量化处理数据及反序列化加载使用的例子

周末宅在家里无所事事,实验了一下如何使用bge对word文档进行向量化处理后并存储到faiss里面供后续反序列化加载使用,下面是具体实现代码。 一,加载word数据并读取内容进行向量化存储 import osimport docxfrom tqdm import tqdmfrom langchain.docstore.document import Documentfrom langc