-
Notifications
You must be signed in to change notification settings - Fork 0
/
gwpca_background_runs.R
63 lines (51 loc) · 1.8 KB
/
gwpca_background_runs.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
library(spdep)
library(sf)
library(GWmodel)
library(ggplot2)
library(tidyverse)
load("/Users/b9047753/Downloads/ismbTutorial.RData")
countsNormCenter <- readRDS(file = "./data/countsNormScaled.rds")
rowDATA <- readRDS(file = "./data/rowDATA.rds")
colDATA <- readRDS(file = "./data/colDATA.rds")
## Prepare for Geographically Weighted PCA (GWPCA)
countsNormCenter <- countsNormCenter %>%
as.data.frame() %>%
rownames_to_column(var = "rowname") %>%
arrange("rowname") %>%
column_to_rownames("rowname")
## Get the coordinates
coords <- colDATA[, c("Barcode", "pixel_x", "pixel_y")] %>%
arrange(Barcode) %>%
column_to_rownames(var = "Barcode")
## Get the data into a SpatialPointsDataFrame object
inputPCAgw <- SpatialPointsDataFrame(coords,
countsNormCenter,
match.ID = TRUE)
## Get the gene names that are going to be evaluated
vars <- colnames(inputPCAgw@data)
## Set the number of components to be retained
k <- 20
## Set the kernel to be used
kernel = "gaussian"
## Set a bandwidth -in pixels- for neighbourhood
dist.Mat <- gw.dist(dp.locat = st_coordinates(colDATA$geom_cntd), p = 2)
print("bw.choice running...")
bw.choice <- bw.gwpca(inputPCAgw,
vars = vars,
k = k,
kernel = kernel,
dMat = dist.Mat,
adaptive = TRUE)
## Save
saveRDS(bw.choice, file = "./data/bw.choice.rds")
# bw.choice <- readRDS(file = "./data/bw.choice.rds")
bw.choice <- 19
print("gwpca running...")
pcaGW <- gwpca(inputPCAgw,
vars = vars,
bw = bw.choice,
k = k,
dMat = dist.Mat,
adaptive = TRUE,
kernel = "gaussian")
saveRDS(pcaGW, file = "./data/pcaGW.rds")