'Unused Arguments' error in mediate function in R - r

I'm trying to run a mediation analysis in R using the mediation package, but I keep getting an error of the type:
Error in mediate(model.m, model.y, sims = 1000, boot = TRUE, treat = "Treat", :
unused arguments (sims = 1000, boot = TRUE, treat = "Treat", mediator = "Med")
The code I'm running is:
model.y <- lm(Perf~Treat*InteractionVar + Med, s)
model.m <- lm(Med~Treat, s)
out.1 <- mediate(model.m, model.y, sims = 1000, boot = TRUE, treat="Treat", mediator = "Med")
I get the same error even if I remove the InteractionVar from the formula too. Any ideas what I'm doing wrong? Thanks!

Related

Got a ' Model is empty!' error when using CIBERSORT for R

when I used CIBERSORT in R to evaluate my data for immune infiltration I got following error form R :
> CIBERSORT_all <- CIBERSORT(LM22.file,exp.file, perm = 1000, QN = T)
Error in predict.svm(ret, xhold, decision.values = TRUE) :
Model is empty!
Called from: predict.svm(ret, xhold, decision.values = TRUE)
and the error located in this function
function (object, newdata, decision.values = FALSE, probability = FALSE, ..., na.action = na.omit)
{...
`if (object$tot.nSV < 1) `
stop("Model is empty!")
...}
but I can't figure out where I got my problem,since I used this function according to the instruction.

Trouble with running h2o.hit_ratio_table

I got this issue when I run H2o for xgboost. May I ask how can I solve this issue? Thank you.
I run this code
h2o.hit_ratio_table(gbm2,valid =T)
And I encounter this error
" Error in names(v) <- v_names :
'names' attribute [1] must be the same length as the vector [0]"
Then I proceed run
mean(finalRF_prediction$predict==test_gb$Cover_Type)
and I got the error:
Error in .h2o.doSafeREST(h2oRestApiVersion = h2oRestApiVersion, urlSuffix = page, :
ERROR MESSAGE:
Name lookup of 'NULL' failed
My model is:
gbm2=h2o.gbm(training_frame = train_gb,validation_frame = valid_gb,x=1:51,y=52,
model_id="gbm2_covType_v2",
ntrees=200,
max_depth = 30,
sample_rate = .7,
col_sample_rate = .7,
learn_rate=.3,
stopping_round=2,
stopping_tolerance = .01,
score_each_iteration = T,seed=2000000)
finalRF_prediction=h2o.predict(object=gbm2,newdata = test_gb)
summary(gbm2)
h2o.hit_ratio_table(gbm2,valid=T)[1,2]
mean(finalRF_prediction$predict==test_gb$Cover_Type)
Without having a dataset to rerun your code on it's hard to say what caused the error. For your second error, check if the column Cover_Type exists in your test_gb dataframe.
The code you have seems to be fine, so I would just double check your column names.
In addition here is a code snippet with xgboost that shows you, you can use the hit_ratio_table() successfully.
library(h2o)
h2o.init()
iris.hex <- h2o.importFile( "http://h2o-public-test-data.s3.amazonaws.com/smalldata/iris/iris_wheader.csv")
i.sid <- h2o.runif(iris.hex)
iris.train <- h2o.assign(iris.hex[i.sid > .2, ], "iris.train")
iris.test <- h2o.assign(iris.hex[i.sid <= .2, ], "iris.test")
iris.xgboost.valid <- h2o.xgboost(x = 1:4, y = 5, training_frame = iris.train, validation_frame = iris.test)
# Hit ratio
hrt.valid.T <- h2o.hit_ratio_table(iris.xgboost.valid,valid = TRUE)
print(hrt.valid.T)

R function slda.em: error when setting the logistic = T argument

I am trying to run supervised LDA on a set of annotated tweets. I keep getting this error msg:
Error in structure(.Call("collapsedGibbsSampler", documents, as.integer(K), : This should not have happened (nan).
How do I fix it?
It only happens when I set logistic = T.
Code below. s is a sample of the data.
corpus1 <- lexicalize(tweets[s], lower=TRUE)
to.keep <- corpus1$vocab[word.counts(corpus1$documents, corpus1$vocab) >= 1]
documents <- lexicalize(tweets[s], lower=TRUE, vocab=to.keep)
params <- sample(c(-1, 1), num.topics, replace=TRUE)
result <- slda.em(documents=documents,
K=num.topics,
vocab=to.keep,
num.e.iterations=10,
num.m.iterations=4,
alpha=1.0, eta=0.1,
annotations = as.integer(annotations[s]),
params,
variance=0.25,
lambda=1.0,
logistic=T)

log2 issue when doing Survival Analysis

I am planning to use TCGAbiolinks to perform an survival analysis by using the following code:
library(TCGAbiolinks)
library(SummarizedExperiment)
query_RNAseq <- GDCquery(project = "TCGA-PAAD",
data.category = "Gene expression",
data.type = "Gene expression quantification",
platform = "Illumina HiSeq",
file.type = "results",
experimental.strategy = "RNA-Seq",
legacy = TRUE)
GDCdownload(query_RNAseq, method = "api",chunks.per.download = 10)
PAADRnaseqSE <- GDCprepare(query_RNAseq)
# Survival Analysis SA
clinical_patient_Cancer <- GDCquery_clinic("TCGA-PAAD","clinical")
dataPAADcomplete <- log2(PAADRnaseqSE)
but it produced an error when I run the log2 function, code and error info are pasted below
> dataPAADcomplete <- log2(PAADRnaseqSE)
Error in log(<S4 object of class "RangedSummarizedExperiment">, 2) :
unused argument (2)
What should I do to solve the problem?

R "Error in terms.formula" using GA/genalg library

I'm attempting to create a genetic algorithm (not picky about library, ga and genalg produce same errors) to identify potential columns for use in a linear regression model, by minimizing -adj. r^2. Using mtcars as a play-set, trying to regress on mpg.
I have the following fitness function:
mtcarsnompg <- mtcars[,2:ncol(mtcars)]
evalFunc <- function(string) {
costfunc <- summary(lm(mtcars$mpg ~ ., data = mtcarsnompg[, which(string == 1)]))$adj.r.squared
return(-costfunc)
}
ga("binary",fitness = evalFunc, nBits = ncol(mtcarsnompg), popSize = 100, maxiter = 100, seed = 1, monitor = FALSE)
this causes:
Error in terms.formula(formula, data = data) :
'.' in formula and no 'data' argument
Researching this error, I decided I could work around it this way:
evalFunc = function(string) {
child <- mtcarsnompg[, which(string == 1)]
costfunc <- summary(lm(as.formula(paste("mtcars$mpg ~", paste(child, collapse = "+"))), data = mtcars))$adj.r.squared
return(-costfunc)
}
ga("binary",fitness = evalFunc, nBits = ncol(mtcarsnompg), popSize = 100, maxiter = 100, seed = 1, monitor = FALSE)
but this results in:
Error in terms.formula(formula, data = data) :
invalid model formula in ExtractVars
I know it should work, because I can evaluate the function by hand written either way, while not using ga:
solution <- c("1","1","1","0","1","0","1","1","1","0")
evalFunc(solution)
[1] -0.8172511
I also found in "A quick tour of GA" (https://cran.r-project.org/web/packages/GA/vignettes/GA.html) that using "string" in which(string == 1) is something the GA ought to be able to handle, so I have no idea what GA's issue with my function is.
Any thoughts on a way to write this to get ga or genalg to accept the function?
Turns out I didn't consider that a solution string of 0s (or indeed, a string of 0s with one 1) would cause the internal paste to read "mpg ~ " which is not a possible linear regression.

Resources