-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
multiple cramer Pb with significative associations
- Loading branch information
Showing
6 changed files
with
200 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
#' Computes Cramer's V for all pairs of one hot encoded levels of input LCZ classifications | ||
#' @param sfInt an sf object with several LCZ classifications to compare on the same intersected geometries, | ||
#' typically an output of the createIntersec function | ||
#' @param columns a vector which contains names of LCZ classification columns | ||
#' @importFrom ggplot2 geom_sf guides ggtitle aes | ||
#' @importFrom caret dummyVars | ||
#' @import sf dplyr cowplot forcats units tidyr RColorBrewer utils grDevices rlang | ||
#' @return an sf file with values of LCZ from all the input | ||
#' are assigned to geometries resulting from intersection of all input geometries | ||
#' @export | ||
#' @examples | ||
multipleCramer<-function(sfInt, columns, nbOutAssociations ){ | ||
if("sf"%in%class(sfInt)){sfInt<-as.data.frame(st_drop_geometry(sfInt))} | ||
sfInt<-sfInt[,columns] | ||
formula_dummy<-reformulate(columns) | ||
sfDummy<-caret::dummyVars(formula = formula_dummy, data = sfInt) %>% predict(newdata = sfInt) %>% as.data.frame | ||
notAlwaysZero<-apply(X = sfDummy, MARGIN = 2, sum)!=0 | ||
sfDummy<-sfDummy[,notAlwaysZero] | ||
# print(summary(sfDummy)) | ||
|
||
|
||
dimVs<-ncol(sfDummy) | ||
Vs<-matrix(ncol = dimVs, nrow = dimVs) | ||
for (i in 1:length(names(sfDummy))){ | ||
for (j in i:length(names(sfDummy))){ | ||
Vs[i,j]<-confintr::cramersv(chisq.test(table(sfDummy[,c(i,j)]), correct = FALSE)) | ||
# print(table(sfDummy[,c(i,j)])) | ||
} | ||
} | ||
rownames(Vs)<-names(sfDummy) | ||
colnames(Vs)<-names(sfDummy) | ||
|
||
threshold <- Vs %>% as.vector %>% unique %>% sort(decreasing=TRUE) %>% head(n = (nbOutAssociations+1)) %>% min | ||
print(paste0("threshold = ", threshold)) | ||
|
||
Mask<-Vs | ||
Mask<-(Mask>=threshold) | ||
Mask[Vs<threshold]<-NA | ||
Mask[Vs==1]<- NA | ||
|
||
keptRows<-apply( | ||
X = apply(X = Mask, MARGIN = 1, is.na), | ||
MARGIN = 1, sum)<ncol(Vs) | ||
keptCols<-apply( | ||
X = apply(X = Mask, MARGIN = 2, is.na ), | ||
MARGIN = 2, sum)<nrow(Vs) | ||
print("keptRows = ") ; print(keptRows) | ||
print("keptCols = ") ; print(keptCols) | ||
signifAssoc<-Vs[keptRows, keptCols] | ||
signifAssoc[(signifAssoc>=1 | signifAssoc<threshold)]<-NA | ||
|
||
output<-list(cramerMatrix = Vs, signifAssoc = signifAssoc) | ||
|
||
return(output) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# This tests the function createIntersect | ||
# library(tinytest) | ||
# | ||
# library(sf) | ||
|
||
sfBDT_11_78030<-importLCZvect(dirPath="/home/gousseff/Documents/3_data/data_article_LCZ_diff_algos/GeoClimate/2011/bdtopo_2_78030/", | ||
file="rsu_lcz.fgb", column="LCZ_PRIMARY") | ||
class(sfBDT_11_78030) | ||
sfBDT_22_78030<-importLCZvect(dirPath="/home/gousseff/Documents/3_data/data_article_LCZ_diff_algos/GeoClimate/2022/bdtopo_3_78030/", | ||
file="rsu_lcz.fgb", column="LCZ_PRIMARY") | ||
sf_OSM_11_Auffargis<-importLCZvect(dirPath="//home/gousseff/Documents/3_data/data_article_LCZ_diff_algos/GeoClimate/2011/osm_Auffargis/", | ||
file="rsu_lcz.fgb", column="LCZ_PRIMARY") | ||
sf_OSM_22_Auffargis<-importLCZvect(dirPath="/home/gousseff/Documents/3_data/data_article_LCZ_diff_algos/GeoClimate/2022/osm_Auffargis/", | ||
file="rsu_lcz.fgb", column="LCZ_PRIMARY") | ||
sf_WUDAPT_78030<-importLCZvect("/home/gousseff/Documents/3_data/data_article_LCZ_diff_algos/WUDAPT/", | ||
file ="wudapt_Auffargis.fgb", column="lcz_primary") | ||
|
||
sfList<-list(BDT11 = sfBDT_11_78030, BDT22 = sfBDT_22_78030, OSM11= sf_OSM_11_Auffargis, OSM22 = sf_OSM_22_Auffargis, | ||
WUDAPT = sf_WUDAPT_78030) | ||
# showLCZ(sfList[[1]]) | ||
|
||
|
||
|
||
intersected<-createIntersect(sfList = sfList, columns = c(rep("LCZ_PRIMARY",4),"lcz_primary"), | ||
sfWf = c("BDT11","BDT22","OSM11","OSM22","WUDAPT")) | ||
names(intersected) %>% reformulate() | ||
|
||
|
||
# test_list<-list(a=c(1,2),b="top",c=TRUE) | ||
# length(test_list) | ||
# for (i in test_list[2:3]) print(str(i)) | ||
|
||
multicompare_test<-compareMultipleLCZ(sfList = sfList, LCZcolumns = c(rep("LCZ_PRIMARY",4),"lcz_primary"), | ||
sfWf = c("BDT11","BDT22","OSM11","OSM22","WUDAPT"),trimPerc = 0.5) | ||
multicompare_test | ||
|
||
test<-multicompare_test$echIntLong | ||
test2<-test %>% subset(agree==TRUE) %>% group_by(LCZvalue) %>% summarize(agreementArea=sum(area)) %>% mutate(percAgreementArea=agreementArea/sum(agreementArea)) | ||
|
||
test<-multicompare_test$echInt[,1:5] %>% st_drop_geometry() | ||
prov1<-apply(X = test, MARGIN = 1, table ) | ||
prov2<-apply(X = test, MARGIN = 1, function(x) max(table(x)) ) | ||
|
||
head(prov1) | ||
head(prov2) | ||
|
||
plot1<-showLCZ(sf = multicompare_test$echInt, column="LCZBDT22", wf="22") | ||
plot2<-showLCZ(sf = multicompare_test$echInt, column="LCZBDT11", wf="11") | ||
|
||
ggplot(data=multicompare_test$echInt) + | ||
geom_sf(aes(fill=nbAgree, color=after_scale(fill)))+ | ||
scale_fill_gradient(low = "red" , high = "green", na.value = NA) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# This tests the function createIntersect | ||
# library(tinytest) | ||
# | ||
# library(sf) | ||
|
||
sfBDT_11_78030<-importLCZvect(dirPath="/home/gousseff/Documents/0_DocBiblioTutosPublis/0_ArticlesScientEtThèses/ArticleComparaisonLCZGCWUDAPTEXPERTS/BDT/2011/bdtopo_2_78030", | ||
file="rsu_lcz.fgb", column="LCZ_PRIMARY") | ||
class(sfBDT_11_78030) | ||
sfBDT_22_78030<-importLCZvect(dirPath="/home/gousseff/Documents/0_DocBiblioTutosPublis/0_ArticlesScientEtThèses/ArticleComparaisonLCZGCWUDAPTEXPERTS/BDT/2022/bdtopo_3_78030", | ||
file="rsu_lcz.fgb", column="LCZ_PRIMARY") | ||
sf_OSM_11_Auffargis<-importLCZvect(dirPath="/home/gousseff/Documents/0_DocBiblioTutosPublis/0_ArticlesScientEtThèses/ArticleComparaisonLCZGCWUDAPTEXPERTS/OSM/2011/osm_Auffargis/", | ||
file="rsu_lcz.fgb", column="LCZ_PRIMARY") | ||
sf_OSM_22_Auffargis<-importLCZvect(dirPath="/home/gousseff/Documents/0_DocBiblioTutosPublis/0_ArticlesScientEtThèses/ArticleComparaisonLCZGCWUDAPTEXPERTS/OSM/2022/osm_Auffargis/", | ||
file="rsu_lcz.fgb", column="LCZ_PRIMARY") | ||
sf_WUDAPT_78030<-importLCZvect("/home/gousseff/Documents/0_DocBiblioTutosPublis/0_ArticlesScientEtThèses/ArticleComparaisonLCZGCWUDAPTEXPERTS/WUDAPT", | ||
file ="wudapt_78030.geojson", column="lcz_primary") | ||
|
||
sfList<-list(BDT11 = sfBDT_11_78030, BDT22 = sfBDT_22_78030, OSM11= sf_OSM_11_Auffargis, OSM22 = sf_OSM_22_Auffargis, | ||
WUDAPT = sf_WUDAPT_78030) | ||
intersected<-createIntersec(sfList = sfList, columns = c(rep("LCZ_PRIMARY",4),"lcz_primary"), | ||
sfWf = c("BDT11","BDT22","OSM11","OSM22","WUDAPT")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# This tests the function multipleCramer | ||
# library(tinytest) | ||
# | ||
# library(sf) | ||
|
||
sfBDT_11_78030<-importLCZvect(dirPath="/home/gousseff/Documents/3_data/data_article_LCZ_diff_algos/GeoClimate/2011/bdtopo_2_78030/", | ||
file="rsu_lcz.fgb", column="LCZ_PRIMARY") | ||
class(sfBDT_11_78030) | ||
sfBDT_22_78030<-importLCZvect(dirPath="/home/gousseff/Documents/3_data/data_article_LCZ_diff_algos/GeoClimate/2022/bdtopo_3_78030/", | ||
file="rsu_lcz.fgb", column="LCZ_PRIMARY") | ||
sf_OSM_11_Auffargis<-importLCZvect(dirPath="//home/gousseff/Documents/3_data/data_article_LCZ_diff_algos/GeoClimate/2011/osm_Auffargis/", | ||
file="rsu_lcz.fgb", column="LCZ_PRIMARY") | ||
sf_OSM_22_Auffargis<-importLCZvect(dirPath="/home/gousseff/Documents/3_data/data_article_LCZ_diff_algos/GeoClimate/2022/osm_Auffargis/", | ||
file="rsu_lcz.fgb", column="LCZ_PRIMARY") | ||
sf_WUDAPT_78030<-importLCZvect("/home/gousseff/Documents/3_data/data_article_LCZ_diff_algos/WUDAPT/", | ||
file ="wudapt_Auffargis.fgb", column="lcz_primary") | ||
|
||
sfList<-list(BDT11 = sfBDT_11_78030, BDT22 = sfBDT_22_78030, OSM11= sf_OSM_11_Auffargis, OSM22 = sf_OSM_22_Auffargis, | ||
WUDAPT = sf_WUDAPT_78030) | ||
# showLCZ(sfList[[1]]) | ||
|
||
|
||
|
||
intersected<-createIntersect(sfList = sfList, columns = c(rep("LCZ_PRIMARY",4),"lcz_primary"), | ||
sfWf = c("BDT11","BDT22","OSM11","OSM22","WUDAPT")) | ||
intersectedSample | ||
|
||
|
||
testVs<-multipleCramer(intersected, columns = names(intersected)[names(intersected)!="geometry"], nbOutAssociations = 5) | ||
testVs$signifAssoc | ||
str(testVs) | ||
|
||
names6<-grep(x = rownames(testVs), pattern = "6$", value = TRUE) | ||
|
||
testVsSampled<-testVs[1:6,1:6] | ||
|
||
testMask<-testVsSampled | ||
ncol(testVsSampled) | ||
nrow(testVsSampled) | ||
testMask<-(testMask>0.004 & testMask<1) | ||
testMask[!testMask]<-NA | ||
|
||
keptRows<-apply( | ||
X = apply(X = testMask, MARGIN = 1, is.na), | ||
MARGIN = 1, sum)<ncol(testVs) | ||
keptCols<-apply( | ||
X = apply(X = testMask, MARGIN = 2, is.na ), | ||
MARGIN = 2, sum)<nrow(testVs) | ||
signifAssoc<-testVsSampled[keptRows, keptCols] | ||
signifAssoc[(signifAssoc>=1 | signifAssoc<0.004)]<-NA | ||
|