R Script for KNN algorithm to run on Tableau - r

I have been writing R script to integrate with tableau. When I try to run KNN using R's dbscan package, it throws the error: K must be greater than 1 and less than the number of samples.
SCRIPT_REAL('
library(dbscan);
lof(as.matrix(data.frame(x= .arg1, y= .arg2)), k= 6)
',
SUM([Measure 1]),
SUM([Measure 2]),
)

Related

How to convert a tensor to an R array (in a loss function, so without eager execution)?

I have TensorFlow version 2.4 and work with the R packages tensorflow (2.2.0) and keras (2.3.0.0.9000).
I would like to convert tensors to R arrays in a loss function (don't ask why).
Here is an example when such a conversion (outside a loss function) works:
library(tensorflow)
library(keras)
x.R <- matrix(1:12, ncol = 3) # dummy R object
x.tensor <- keras_array(x.R) # converting the R object to a tensor
as.array(x.tensor) # converting it back to an R array. This works because...
stopifnot(tf$executing_eagerly()) # ... eager execution is enabled
During training of a model, eager execution is FALSE though and thus
the as.array() call fails. To see this, let's first define a dummy
neural network model and training data.
d <- 2 # input and output dimension
in.lay <- layer_input(shape = d)
hid.lay <- layer_dense(in.lay, units = 300, activation = "relu")
out.lay <- layer_dense(hid.lay, units = d, activation = "sigmoid")
model <- keras_model(in.lay, out.lay)
n <- 1200 # number of training samples
data <- matrix(runif(n * d), ncol = d) # training data
Now let's define the loss function and compile the model with it.
myloss <- function(x, y) { # x and y are tensors here
stopifnot(!tf$executing_eagerly()) # confirms that eager execution is disabled
x. <- as.array(x) # ... fails with "RuntimeError: Evaluation error: invalid first argument, must be vector (list or atomic)." How can we convert x to an R array?
loss_mean_squared_error(x, y) # just a dummy return value (the MSE)
}
compile(model, optimizer = "adam", loss = myloss)
Let's try and fit this model (to see that it fails to convert the tensor x to an R array via as.array()).
prior <- matrix(rexp(n * d), ncol = d) # input sample to train the NN on
n.epoch <- 5 # number of epochs to train
batch.size <- 400 # batch size
fit(model, x = prior, y = data, batch_size = batch.size, epochs = n.epoch) # fails with error message given above
The R package tensorflow provides tfe_enable_eager_execution() to enable eager execution
in a session. But if I call it with TensorFlow 2.4, then I obtain:
tfe_enable_eager_execution() # "Error in py_get_attr_impl(x, name, silent) : AttributeError: module 'tensorflow' has no attribute 'contrib'"
Ideally, I wouldn't want to mess with eager execution much (not sure about the side effects),
just converting a tensor to an array. My guess is that there is no other way than eager execution as
only then the pointers are resolved and the R package tensorflow finds the data
in the tensor and is able to convert it to an array.
Other ideas to enable/disable eager execution are mentioned here but that's all in Python
and not available in R it seems. And this this post seems to ask the same question but in a different context.

Error in R long vectors not supported yet

I am using CentOS 7 Linux compute cluster, with 130 GB RAM. I am trying to use the SVM function from the e1071 R package. My matrix dimension is rows = 350 and columns = 54250.
R script Code (file_testR.R)
matris=matrix(rnorm(100),350,54251)
matris <- as.data.frame(matris)
matris$new_variable <- 0
matris$new_variable[1:175] <- "yes"
matris$new_variable[176:350] <- "no"
require(e1071)
svmfit_test <- svm(as.factor(matris$new_variable)~., data = matris, kernel = "linear", cross=10)
Bash code
Rscript --max-ppsize=500000 file_testR.R
I am getting this below error:
Error in model.matrix.default(Terms, m) :
long vectors not supported yet: ../../src/include/Rinlinedfuns.h:522
Calls: svm ... svm.formula -> model.matrix -> model.matrix.default
I would appreciate it if anybody could help me to understand this issue.

plotmplier in NARDL package

I have a problem with running plotmplier command in NARDL package. I keep receiving the same error:
error in plot.window(…) :need finite ylim
erd22 <- nardl(ef~lwr,efo,ic="bic",maxlags = TRUE,graph = FALSE,case=3) summary(erd22)
plotmplier(erd22,2,2,10)
Result through NARDL in R Prog is contradicted with Stata and Eviews. F Bound test is not a correct one. Better to use Stata or eviews 9/10.

sem() function Error in w_mat %*% p_deriv_mat %*% invMat

I ran into the following error when running the sem() function:
I have just ran this code using a different syntax and it worked; so I know the problem should be either in the way I loaded the factors to the syntax... or a typo?
library(psych)
library(sem)
#Creating syntax equation
mgb_syn_eq <- "
MIL: S.Defense #Military and National Security
GOV: E.GovLess, E.GovFM, E.GovBig #Limited Government
BUS: E.BusinessReg #Business
"
#Creating CFA Syntax from theory
mgb_syn <- cfa(text = mgb_syn_eq, reference.indicators = FALSE)
#Use the sem() function to run a CFA
mgb_CFA <- sem(mgb_syn, data = df.secs_CFA)
Error in w_mat %% p_deriv_mat %% invMat : requires numeric/complex matrix/vector arguments
I found the answer:
The problem was that psych package only has some of the basic functions of the lavaan package AND that I was trying to estimate a latent value with only 1 parameter (you need at least 3).

could not find function "FUNcluster" in R

I want to run kmeans clustering on my data and show the plot using this code:
Elbow method is used to calculate number of k.
library(tidyverse) # data manipulation
library(cluster) # clustering algorithms
library(factoextra) # clustering algorithms & visual
library(NbClust) #use zip file to install it
wss <- function(k) {
kmeans(df_km, k, nstart = 25 )$tot.withinss
}
# Compute and plot wss for k = 1 to k = 32
k.values <- 1:32
wss_values <- map_dbl(k.values, wss)
set.seed(123)
fviz_nbclust(df_km, FUNcluster = kmeans, method = "wss")
in the last line of code, it shows this error:
Error in FUNcluster(x, i, ...) : could not find function "FUNcluster"
I tried to restart session, also used .zip, CRAN, and also this link to download the "factoextra"
devtools::install_github("kassambara/factoextra")
But still I get the error. Any solution to solve it?

Resources