I'm working on a clustering problem in sparklyr
. Many of the variables in the training set are measured on different scales and thus differ by orders of magnitude. Per best practice I am trying to scale and center the data.
There are a number of different formulas to do this, the most traditional being (X - µ) / σ where X is the random variable , µ= mean, and σ= standard deviation. I tend like to also use (X - x) / (x_max - x_min) where X= random variable, x=sample mean, x_max= maximum value, and x_min =minimum value.
I am getting a wierd result after applying this transformation using dplyr
. Consider this example:
#connect to spark
library(sparklyr)
library(SparkR)
library(dplyr)
sc = spark_connect(master = 'yarn-client',
spark_home = '/usr/hdp/current/spark-client',
app_name = 'sparklyr'
# config = list(
# "sparklyr.shell.executor-memory" = "XG",
# "sparklyr.shell.driver-memory" = "XG",
# "spark.driver.maxResultSize" = "XG" # may need to transfer a lot of data into R
)
sparkR.init()
#create a dataframe where variables in the dataset differ by an order of magnitude
mat <- as.data.frame(matrix(data = rnorm(200, mean=100,sd=10), nrow = 1000, ncol = 50))
mat1 <- as.data.frame(matrix(data = rnorm(200, mean=0,sd=1), nrow = 1000, ncol = 50))
colnames(mat1) <- paste('X',1:50,sep='')
mat.final <- cbind(mat,mat1)
#copy to Spark
dat.out <- sdf_copy_to(sc,mat.final,'dat',overwrite=TRUE)
#define centering and scaling function
scale.center <- function(x){
(x-mean(x,na.rm=TRUE)) /(max(x,na.rm = TRUE)-min(x,na.rm = TRUE))
}
#scale data
dat.out1 <-
dat.out %>%
mutate_each(funs(s=scale.center))
The code runs , but I get something strange:
str(dat.out1)
$ ops:List of 4
..$ name: chr "mutate"
..$ x :List of 4
.. ..$ name: chr "mutate"
.. ..$ x :List of 4
.. .. ..$ name: chr "mutate"
.. .. ..$ x :List of 4
.. .. .. ..$ name: chr "mutate"
.. .. .. ..$ x :List of 4
.. .. .. .. ..$ name: chr "mutate"
.. .. .. .. ..$ x :List of 4
.. .. .. .. .. ..$ name: chr "mutate"
.. .. .. .. .. ..$ x :List of 4
.. .. .. .. .. .. ..$ name: chr "mutate"
.. .. .. .. .. .. ..$ x :List of 4
.. .. .. .. .. .. .. ..$ name: chr "mutate"
.. .. .. .. .. .. .. ..$ x :List of 4
.. .. .. .. .. .. .. .. ..$ name: chr "mutate"
.. .. .. .. .. .. .. .. ..$ x :List of 4
.. .. .. .. .. .. .. .. .. ..$ name: chr "mutate"
.. .. .. .. .. .. .. .. .. ..$ x :List of 4
.. .. .. .. .. .. .. .. .. .. ..$ name: chr "mutate"
.. ..
The above is just a portion of the output after running str
. Thoughts on what's going wrong here. I'm surprised there isn't a build in function for centering and scaling.