R语言聚类分析因子分析t检验程序.docx

上传人:b****3 文档编号:24852127 上传时间:2023-06-02 格式:DOCX 页数:12 大小:17.83KB
下载 相关 举报
R语言聚类分析因子分析t检验程序.docx_第1页
第1页 / 共12页
R语言聚类分析因子分析t检验程序.docx_第2页
第2页 / 共12页
R语言聚类分析因子分析t检验程序.docx_第3页
第3页 / 共12页
R语言聚类分析因子分析t检验程序.docx_第4页
第4页 / 共12页
R语言聚类分析因子分析t检验程序.docx_第5页
第5页 / 共12页
点击查看更多>>
下载资源
资源描述

R语言聚类分析因子分析t检验程序.docx

《R语言聚类分析因子分析t检验程序.docx》由会员分享,可在线阅读,更多相关《R语言聚类分析因子分析t检验程序.docx(12页珍藏版)》请在冰豆网上搜索。

R语言聚类分析因子分析t检验程序.docx

R语言聚类分析因子分析t检验程序

R语言聚类分析、因子分析、t检验相关程序及程序运行结果相关程序:

#####读入数据

x=read.delim("G:

\\上机考试数据.txt",header=TRUE,row.names=1)

####作系统聚类

d=dist(scale(x))

hc1=hclust(d);hc1

hc2=hclust(d,"average")

hc3=hclust(d,"centroid")

hc4=hclust(d,"ward")

####绘出谱系图和聚类情况(最长距离法、类平均法)

opar=par(mfrow=c(2,1),mar=c(5.2,4,0,0))

plclust(hc1,hang=-1)

re1=rect.hclust(hc1,k=3,border="red")

plclust(hc2,hang=-1)

re2=rect.hclust(hc2,k=3,border="red")

par(opar)

####绘出谱系图和聚类情况(重心法和Ward法)

opar<-par(mfrow=c(2,1),mar=c(5.2,4,0,0))

plclust(hc3,hang=-1)

re3=rect.hclust(hc3,k=3,border="red")

plclust(hc4,hang=-1)

re4=rect.hclust(hc4,k=3,border="red")

par(opar)

####动态聚类法

km<-kmeans(scale(x),3,nstart=35);km

sort(km$cluster)

####因子分析

y=read.delim("G:

\\上机考试数据.txt",header=TRUE,row.names=1)

R=cov(scale(y))

fa<-factanal(factors=4,covmat=R);fa

####计算因子得分

y=read.delim("G:

\\上机考试数据.txt",header=TRUE,row.names=1)

fa<-factanal(~.,factors=4,data=y,scores="Bartlett");fa

fa$scores####输出因子得分

####画出散点图

plot(fa$scores[,1:

2],type="n")

text(fa$scores[,1],fa$scores[,2])

plot(fa$scores[,3:

4],type="n")

text(fa$scores[,3],fa$scores[,4])

####t检验

a1=fa$scores[,1]

a2=fa$scores[,2]

a3=fa$scores[,3]

a4=fa$scores[,4]

t.test(a1,a2,alternative="greater")

t.test(a1,a3,alternative="greater")

t.test(a1,a4,alternative="greater")

t.test(a2,a3,alternative="greater")

t.test(a2,a4,alternative="greater")

t.test(a3,a4,alternative="greater")

程序运行结果:

>rm(list=ls(all=TRUE))

>#####读入数据

>x=read.delim("G:

\\上机考试数据.txt",header=TRUE,row.names=1)>####作系统聚类

>d=dist(scale(x))

>hc1=hclust(d);hc1

Call:

hclust(d=d)

Clustermethod:

complete

Distance:

euclidean

Numberofobjects:

35

>hc2=hclust(d,"average")

>hc3=hclust(d,"centroid")

>hc4=hclust(d,"ward")

>####绘出谱系图和聚类情况(最长距离法、类平均法)

>opar=par(mfrow=c(2,1),mar=c(5.2,4,0,0))

>plclust(hc1,hang=-1)

>re1=rect.hclust(hc1,k=3,border="red")

>plclust(hc2,hang=-1)

>re2=rect.hclust(hc2,k=3,border="red")

>par(opar)

>####绘出谱系图和聚类情况(重心法和Ward法)

>opar<-par(mfrow=c(2,1),mar=c(5.2,4,0,0))

>plclust(hc3,hang=-1)

>re3=rect.hclust(hc3,k=3,border="red")

>plclust(hc4,hang=-1)

>re4=rect.hclust(hc4,k=3,border="red")

>par(opar)

>####动态聚类法

>km<-kmeans(scale(x),3,nstart=35);km

K-meansclusteringwith3clustersofsizes21,6,8

Clustermeans:

工业生产总值.亿元.财政收入.万元.人均财政收入社会消费品零售总额.万元.

1-0.6065578-0.6358116-0.492914324-0.38769223

20.70213300.95658061.73019078

30.06157708

31.06561460.9515700-0.0037429870.97150928

外贸出口额外资利用总额.万美元.新增固定资产投资.万元.职工平均工资.元.

1-0.4402834-0.6033739-0.5009169-0.5949817

20.430951

31.46478750.877016

41.4748054

30.83253060.48526590.65714450.4557230

农民人均纯收入.元.城镇固定资产投资人均固定资产投资万人拥有工业企业数量

1-0.5985518-0.6641800-0.50088606-0.4006611

21.35346171.11060891.784768221.5715357

30.55610210.9105157-0.02375026-0.1269165人均科教文卫.事业费支出

1-0.21817624

20.80904765

3-0.03407311

Clusteringvector:

长丰县肥东县肥西县天长市明光市来安县全椒县定远县

33331111

凤阳县当涂县庐江县无为县含山县和县芜湖县繁昌县

12131322

南陵县宁国市郎溪县广德县泾县绩溪县旌德县铜陵县

32121112

东至县石台县青阳县桐城市怀宁县枞阳县潜山县太湖县

11131111

宿松县望江县岳西县

111

Withinclustersumofsquaresbycluster:

[1]91.9507140.3227274.39793

(between_SS/total_SS=53.2%)

Availablecomponents:

[1]"cluster""centers""totss""withinss"

[5]"tot.withinss""betweenss""size"

>sort(km$cluster)

明光市来安县全椒县定远县凤阳县庐江县含山县郎溪县

11111111

泾县绩溪县旌德县东至县石台县青阳县怀宁县枞阳县

11111111

潜山县太湖县宿松县望江县岳西县当涂县芜湖县繁昌县

11111

222

宁国市广德县铜陵县长丰县肥东县肥西县天长市无为县

222

33333

和县南陵县桐城市

333

>

>####因子分析

>y=read.delim("G:

\\上机考试数据.txt",header=TRUE,row.names=1)

>R=cov(scale(y))

>fa<-factanal(factors=4,covmat=R);fa

Call:

factanal(factors=4,covmat=R)

Uniquenesses:

工业生产总值.亿元.财政收入.万元.人均财政收入

0.1310.0050.017社会消费品零售总额.万元.外贸出口额外资利用总额.万美元.

0.1350.7570.466新增固定资产投资.万元.职工平均工资.元.农民人均纯收入.元.

0.2780.2490.228

城镇固定资产投资人均固定资产投资万人拥有工业企业数量

0.0050.0140.213人均科教文卫.事业费支出

0.518

Loadings:

Factor1Factor2Factor3Factor4

工业生产总值.亿元.0.1640.8630.306

财政收入.万元.0.2700.9040.2520.204

人均财政收入0.9110.2570.2360.177

社会消费品零售总额.万元.-0.3340.6620.5290.188

外贸出口额0.1440.2550.396

外资利用总额.万美元.0.4180.4410.405

新增固定资产投资.万元.0.1710.4850.675

职工平均工资.元.0.6690.4460.322

农民人均纯收入.元.0.5280.4530.5010.192

城镇固定资产投资0.3040.8930.293-0.142

人均固定资产投资0.8880.2720.263-0.231

万人拥有工业企业数量0.8380.271

人均科教文卫.事业费支出0.659-0.197

Factor1Factor2Factor3Factor4

SSloadings4.0093.8381.8910.247

ProportionVar0.3080.2950.1450.019

CumulativeVar0.3080.6040.7490.768

Thedegreesoffreedomforthemodelis32andthefitwas1.9244

>

>####计算因子得分

>y=read.delim("G:

\\上机考试数据.txt",header=TRUE,row.names=1)

>fa<-factanal(~.,factors=4,data=y,scores="Bartlett");fa

Call:

factanal(x=~.,factors=4,data=y,scores="Bartlett")

Uniquenesses:

工业生产总值.亿元.财政收入.万元.人均财政收入

0.1310.0050.017社会消费品零售总额.万元.外贸出口额外资利用总额.万美元.

0.1350.7570.466新增固定资产投资.万元.职工平均工资.元.农民人均纯收入.元.

0.2780.2490.228

城镇固定资产投资人均固定资产投资万人拥有工业企业数量

0.0050.0140.213人均科教文卫.事业费支出

0.518

Loadings:

Factor1Factor2Factor3Factor4

工业生产总值.亿元.0.1640.8630.306

财政收入.万元.0.2700.9040.2520.204

人均财政收入0.9110.2570.2360.177

社会消费品零售总额.万元.-0.3340.6620.5290.188

外贸出口额0.1440.2550.396

外资利用总额.万美元.0.4180.4410.405

新增固定资产投资.万元.0.1710.4850.675

职工平均工资.元.0.6690.4460.322

农民人均纯收入.元.0.5280.4530.5010.192

城镇固定资产投资0.3040.8930.293-0.142

人均固定资产投资0.8880.2720.263-0.231

万人拥有工业企业数量0.8380.271

人均科教文卫.事业费支出0.659-0.197

Factor1Factor2Factor3Factor4

SSloadings4.0093.8381.8910.247

ProportionVar0.3080.2950.1450.019

CumulativeVar0.3080.6040.7490.768

Testofthehypothesisthat4factorsaresufficient.

Thechisquarestatisticis50.36on32degreesoffreedom.

Thep-valueis0.0206

>fa$scores####输出因子得分

Factor1Factor2Factor3Factor4长丰县-0.1839626451.07142140-0.80273922-0.55908770肥东县-0.1780912072.78806120-1.869996990.68363597肥西县0.3486745953.12457544-1.98272073-0.50108034天长市-0.3286629390.111595891.527366470.46407338明光市-0.963771018-0.863549660.470027760.81176717来安县-0.396419372-0.490653540.31428314-0.67509321全椒县-0.127042257-0.419889530.37755024-0.37704456定远县-1.024098347-0.26034805-0.17002711-0.38265932

凤阳县-0.688874840-0.11971576-0.149323670.36608860当涂县0.7037171531.670270290.790349650.31755372庐江县-1.347580892-0.102963821.402392140.73197698无为县-1.8088349901.385048182.96739685-0.89328453含山县-0.097182092-0.62906939-0.017257820.69586421和县-0.633409518-0.226670391.11273582-0.23828640芜湖县1.4658235810.209259370.70918377-0.98730471繁昌县2.663194026-0.03934722-0.096996981.76758867南陵县-0.121028895-0.240599561.317465061.09467594宁国市2.1760513040.261465912.182125960.98305592郎溪县0.322546258-0.51458155-0.18277513-2.09016770

广德县0.4589008880.384439021.34637112-1.52375887泾县0.066399254-0.786674840.12591414-0.17464409绩溪县1.630369791-1.376421080.12958686-1.09105223旌德县0.015581604-1.38966324-0.22472503-0.02326432

铜陵县2.3526855060.08194171-0.90854462-1.71218909东至县-0.420839170-0.45254138-0.451577590.05270322石台县0.004431299-1.12401413-1.869384040.42232027青阳县0.395060735-0.98715620-0.516652482.04123624桐城市-0.3598737840.448872710.569349520.74475186怀宁县-0.0229618630.38101100-0.979917222.45423594枞阳县-0.6667594010.68399096-1.01032266-0.08880833潜山县-0.659551399-0.59777412-0.595834070.51402884太湖县-0.654810156-0.58593199-0.98967268-0.43277938

宿松县-0.920198188-0.28228760-0.27058925-0.98831067望江县-0.825113048-0.70444337-0.56674157-0.10014261岳西县-0.174369973-0.40765667-1.68629963-1.30659888>####画出散点图

>plot(fa$scores[,1:

2],type="n")

>text(fa$scores[,1],fa$scores[,2])

>plot(fa$scores[,3:

4],type="n")

>text(fa$scores[,3],fa$scores[,4])

>

>####t检验

>a1=fa$scores[,1]

>a2=fa$scores[,2]

>a3=fa$scores[,3]

>a4=fa$scores[,4]

>t.test(a1,a2,alternative="greater")

>t.test(a11,b11,alternative="greater")

WelchTwoSamplet-test

data:

a11andb11

t=1.9772,df=9.704,p-value=0.03855

alternativehypothesis:

truedifferenceinmeansisgreaterthan095percentconfidenceinterval:

0.06604808Inf

sampleestimates:

meanofxmeanofy

0.99583130.1749454

>t.test(a11,c11,alternative="greater")

WelchTwoSamplet-test

data:

a11andc11

t=4.3788,df=9.086,p-value=0.0008671

alternativehypothesis:

truedifferenceinmeansisgreaterthan095percentconfidenceinterval:

1.039104Inf

sampleestimates:

meanofxmeanofy

0.9958313-0.7901305

>t.test(b11,c11,alternative="greater")

WelchTwoSamplet-test

data:

b11andc11

t=5.8762,df=21.925,p-value=3.299e-06

alternativehypothesis:

truedifferenceinmeansisgreaterthan095percentconfidenceinterval:

0.6830173Inf

sampleestimates:

meanofxmeanofy

0.1749454-0.7901305

>t.test(a12,b12,alternative="greater")

WelchTwoSamplet-test

data:

a12andb12

t=2.994,df=10.814,p-value=0.006212

alternativehypothesis:

truedifferenceinmeansisgreaterthan095percentconfidenceinterval:

0.5557607Inf

sampleestimates:

meanofxmeanofy

0.9931852-0.3988946

>t.test(a12,c12,alternative="greater")

WelchTwoSamplet-test

data:

a12andc12

t=2.5917,df=10.145,p-value=0.01329

alternativehypothesis:

truedifferenceinmeansisgreaterthan095percentconfidenceinterval:

0.356743Inf

sampleestimates:

meanofxmeanofy

0.9931852-0.1893416

>t.test(b12,c12,alternative="greater")

WelchTwoSamplet-test

data:

b12andc12

t=-0.8809,df=22.84,p-value=0.8062

alternativehypothesis:

truedifferenceinmeansisgreaterthan095percentconfidenceinterval:

-0.6173701Infsampleestimates:

meanofxmeanofy-0.3988946-0.1893416

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 考试认证 > IT认证

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1