본문 바로가기

R프로그래밍,통계학

0324.

반응형

#2030대아이폰 사용자들의 평균 vs 그외사용자들의 평균비교해보자

#######!! 그 전에 다른 변수들도 평균 떄려서 카테고리당 하나의 값나오게 합쳐보자(일단남자기준)

mprofile <-profile[profile$X0001==1] # 남자만 추림.
ni_mprofile <-mprofile[mprofile$J0081==0] # 아이폰 + 남자만 추림
I_mprofile<-mprofile[mprofile$J0081==0]
#View(head(ni_mprofile))

ni_mskin <- ni_mprofile[,c(149:160)]  # 아이폰_남자피부관리
sum(is.na(ni_mskin)) #na있는지 확인 : 0  
ni_mskin_mean <- apply(ni_mskin,1,mean)
View(head(ni_mskin_mean))


I_mskin_mean <-ni_mskin_mean

I_mfood <- I_mprofile[,c(270:339)]
View(head(I_mfood))
sum(is.na(I_mfood)) # NA 하나도 없음 #0
I_mfood_mean <- apply(I_mfood,1,mean)

I_mhealth<-I_mprofile[,c(340:396)]
View(head(I_mhealth))
sum(is.na(I_mhealth)) #NA 하나도 없음
I_mhealth_mean<-apply(I_mhealth,1,mean)

cor(I_mfood_mean,I_mhealth_mean) # 0.6074 vs 0.596
cor(I_mskin_mean,I_mfood_mean)  # 0.5655841 vs 0.526
cor(I_mskin_mean,I_mhealth_mean) # 0.48922 vs 0.476

I_x <-data.frame(I_mfood_mean,I_mskin_mean,I_mhealth_mean)
nrow(I_x)
cor(I_x)
symnum(cor(I_x))#
install.packages("corrgram")
library(corrgram)
corrgram(I_x)
corrgram(I_x,upper.panel=panel.conf)

corrgram(cor()) # 일단 피부, 헬스, 음식 상관관계 보기 완료
#이3개 변수만으로 군집분석 돌려보자. (아이폰,비아이폰 통합데이터)
I_clusters <- kmeans(I_x,centers= 3)
head(I_clusters[I_clusters$cluster=="2"])


length(I_clusters[I_clusters$cluster==1]) #13958
length(I_clusters[I_clusters$cluster==2]) #15004
length(I_clusters[I_clusters$cluster==3]) #5030
###와...드디어 성공했다!!!!!!!!!!
13958 + 15004 + 5030 # 33992


I_clusters$centers

class(I_x)
plot(I_x,col=I_clusters$cluster) #I_x 는 data.frame
points(I_x$centers,col=1:3,pch=18,cex=12)

#q. 군집분석으로 나눈 군집을 선택하여 따로 분석에 사용할 수는 없을까 ? 
#rpub 예제 해보기!!

반응형