각 관찰에 대해 가장 가까운 클러스터를 찾기 위해 두 데이터 프레임의 행 사이의 거리 (비 유사성)를 계산하고 싶습니다. 요인과 수치 변수가 있으므로 Gower 거리를 사용하고 있습니다. 하나의 행렬의 행 사이의 차이점이 아닌 두 개의 데이터 프레임을 비교하려는 경우 gower.dist가 필요한 함수가됩니다. 그러나 구현할 때 데이지의 거버 (gower)를 사용할 때 결과가 다르다는 것을 깨달았습니다. 행을 묶고 관심있는 비평 행렬 부분을 살펴 보았습니다.R - 다른 결과 gower.dist 및 daisy (..., metric = "gower")
여기서는 내 데이터 샘플 만 제공하지만 모든 데이터와 비 유사성을 계산할 때 gower.dist는 해당 행이 서로 같지 않아도 0의 비 유사성을 가져 오는 경우가 많았습니다. 왜? 그리고 그 결과가 다른 이유는 무엇일까요? 제 생각에, daisys의 집은 올바르게 작동하고 gower.dist는 (이 예제에서는) 작동하지 않습니다. 다음 데이터
df <- structure(list(searchType = structure(c(NA, 1L, 1L, 1L, 1L), .Label = c("1", "2"), class = "factor"), roomMin = structure(c(4L, 1L, 1L, 6L, 6L), .Label = c("10", "100", "150", "20", "255", "30", "40", "50", "60", "70", "Missing[NoInput]"), class = "factor"), roomMax = structure(c(8L, 8L, NA, 10L, 9L), .Label = c("10", "100", "120", "150", "160", "20", "255", "30", "40", "50", "60", "70", "80", "90", "Missing[NoInput]"), class = "factor"), priceMin = c(NA, 73, 60, 29, 11), priceMax = c(35, 11, 1, 62, 23), sizeMin = structure(c(5L, 5L, 5L, 6L, 6L), .Label = c("100", "125", "150", "250", "50", "75", "Missing[NoInput]"), class = "factor"), sizeMax = structure(c(1L, 6L, 5L, 3L, 1L), .Label = c("100", "125", "150", "250", "50", "75", "Missing[NoInput]"), class = "factor"), longitude = c(6.6306, 7.47195, 8.5562, NA, 8.569), latitude = c(46.52425, 46.9512, 47.37515, NA, 47.3929), specificSearch = structure(c(1L, 1L, 1L, 1L, 1L), .Label = c("0", "1"), class = "factor"), objectType = structure(c(NA, 2L, 2L, 2L, 2L), .Label = c("1", "2", "3", "Missing[]"), class = "factor")), .Names = c("searchType", "roomMin", "roomMax", "priceMin", "priceMax", "sizeMin", "sizeMax", "longitude", "latitude", "specificSearch", "objectType"), row.names = c(112457L, 94601L, 78273L, 59172L, 117425L), class = "data.frame")
cent <- structure(list(searchType = structure(c(1L, 1L, 1L), .Label = c("1", "2"), class = "factor"), roomMin = structure(c(1L, 4L, 4L), .Label = c("10", "100", "150", "20", "255", "30", "40", "50", "60", "70", "Missing[NoInput]"), class = "factor"), roomMax = structure(c(6L, 9L, 8L), .Label = c("10", "100", "120", "150", "160", "20", "255", "30", "40", "50", "60", "70", "80", "90", "Missing[NoInput]"), class = "factor"), priceMin = c(60, 33, 73), priceMax = c(103, 46, 23), sizeMin = structure(c(1L, 5L, 5L), .Label = c("100", "125", "150", "250", "50", "75", "Missing[NoInput]"), class = "factor"), sizeMax = structure(c(1L, 2L, 1L), .Label = c("100", "125", "150", "250", "50", "75", "Missing[NoInput]"), class = "factor"), longitude = c(8.3015, 7.42765, 7.6104), latitude = c(47.05485, 46.9469, 46.75125), specificSearch = structure(c(1L, 1L, 1L), .Label = c("0", "1"), class = "factor"), objectType = structure(c(2L, 2L, 2L), .Label = c("1", "2", "3", "Missing[]"), class = "factor")), .Names = c("searchType", "roomMin", "roomMax", "priceMin", "priceMax", "sizeMin", "sizeMax", "longitude", "latitude", "specificSearch", "objectType"), row.names = c(60656L, 66897L, 130650L), class = "data.frame")
은 감사와
library(cluster)
library(StatMatch)
# Calculate distance using daisy's gower
daisyDist <- daisy(rbind(df,cent),metric="gower")
daisyDist <- as.matrix(daisyDist)
daisyDist <- daisyDist[(nrow(df)+1):nrow(daisyDist),1:nrow(df)] #only look at part where rows from df are compared to (rows of) cent
# Calculate distance using dist.gower
gowerDist <- gower.dist(cent,df)
!
EDIT : 숫자 열에 NAs가 있기 때문에 오류/차이가 발생하고 다르게 처리되는 것으로 보입니다. 데이지의 GA 처리 방법을 gower.dist에 어떻게 적용 할 수 있습니까?
HTTP : //stats.stackexchange.c om/questions/123624/gower-distance-with-r-functions-gower-dist-and-daisy가 도움이 될 것입니다. –
링크를 이용해 주셔서 감사합니다. 그러나, 그것은 정말로 도움이되지 않습니다. 문제는 다른 것입니다. 그리고 같은 거리 행렬을 가져야한다는 것을 알고 있다는 것도 도움이되지 않습니다. 제 변수는 타입 요소와 숫자로되어 있습니다. 둘 다 데이지와 고어에서 동등하게 취급되어야합니다. .dist – Vanessa