optim is giving errors from par input where fn = dlogis - r

I don't know if this should be a new item or not.
I modified the code to be:
library(Rcpp)
rm(list = ls())
datafile <- data.frame(
N = c(1.5, 2.6, 0.555555555555556, 0.535714285714286, 0.418604651162791, 0.557377049180328, 0.463157894736842, 0.762589928057554, 0.583673469387755, 0.528350515463918, 0.649241146711636, 0.534764826175869, 0.556295802798135, 0.250856164383562, 0.202258726899384, 0.351266723598064, 0.226669475458184, 0.1275974583548, 0.0906183368869936, 0.123027510124284, 0.119124595871674)
)
View(datafile)
datafile$a<-dlogis(datafile$N, location = 0, scale = 1, log = FALSE)
datafile does not become 2 columns N rows. It becomes 1 column N+1 rows where the datafile$a becomes one more entry.
I am wondering if anyone would know whether this is the problem my code with dlogis or with optim.
1) Console says I am running dlogis correct.
Console at the same time says I am not running it correctly when I call optim with dlogis. What is the correct way to call optim with the below code?
2) Also, when I call dlogis, I want to find the parameters location and scale so that the errors with the data I feed in can be minimized. Are there other things I should pay attention?
Thank you
library(Rcpp)
rm(list = ls())
datafile <- data.frame(
N = c(1.5, 2.6, 0.555555555555556, 0.535714285714286, 0.418604651162791, 0.557377049180328, 0.463157894736842, 0.762589928057554, 0.583673469387755, 0.528350515463918, 0.649241146711636, 0.534764826175869, 0.556295802798135, 0.250856164383562, 0.202258726899384, 0.351266723598064, 0.226669475458184, 0.1275974583548, 0.0906183368869936, 0.123027510124284, 0.119124595871674)
)
View(datafile)
datafile$a<-dlogis(datafile$N, location = 0, scale = 1, log = FALSE)
View(datafile)
optim(c(datafile$N, 0.5, 0.1), dlogis)
Console gives me this error msg:
> optim(c(datafile$N, 0.5, 0.1), dlogis)
Error in optim(c(datafile$N, 0.5, 0.1), dlogis) :
objective function in optim evaluates to length 23 not 1

Related

Error calling rCBA::fpgrowth: method fpgrowth with signature (DDI)[[Ljava/lang/String; not found

I wrote the R code below to mine with the FP-Growth algorithm:
fpgabdata <- read.csv('../Agen Biasa.csv', header = FALSE)
train <- sapply(fpgabdata, as.factor)
train <- data.frame(train, check.names = TRUE)
txns <- as(train,"transactions")
abrulesfpg = rCBA::fpgrowth(txns, support = 0.25, confidence = 0.5, maxLength = 10, consequent = NULL, verbose = TRUE, parallel = TRUE)
But I get the following error:
Error in .jcall(jPruning, "[[Ljava/lang/String;", "fpgrowth", support, :
method fpgrowth with signature (DDI)[[Ljava/lang/String; not found
These are my data:
The reason you are seeing this error is that the current implementation of the FP-growth algorithm in rCBA requires that you specify a value for the consequent (right hand side).
For example, the following should work, assuming you have sensible thresholds for support and confidence:
abrulesfpg = rCBA::fpgrowth(
txns,
support = 0.25,
confidence = 0.5,
maxLength = 10,
consequent = "SPIRULINA",
verbose = TRUE,
parallel = TRUE
)
I know the OP is likely to have discovered this by now, but I've answered this just in case anyone else encounters the same error.

xgb.plot.multi.trees What is the meaning of the numbers in the parentheses?

It isn't number of trees, since I only trained 25. It also isn't the value of the variable. This is evident by the scale of the values in the parenthesis, which doesn't make sense since many variables are logged. I checked the documentation and there was no explanation. Any ideas or other references?
df1 <- xgb.train(data = X_train_dmat,
eta = 0.1,
max_depth = 5,
nround=25,
subsample = 0.5,
colsample_bytree = 0.5,
booster = 'gbtree',
objective = 'reg:squarederror',
nthread = 3
)
xgb.plot.multi.trees(model = df1,
features_keep = 5,
use.names=FALSE,
plot_width = NULL,
plot_height = NULL,
render = TRUE
)
Looking at the source code, https://github.com/dmlc/xgboost/blob/master/R-package/R/xgb.plot.multi.trees.R#L94, this is the part creating the nodes in the tree:
nodes.dt <- tree.matrix[
, .(Quality = sum(Quality))
, by = .(abs.node.position, Feature)
][, .(Text = paste0(Feature[1:min(length(Feature), features_keep)],
" (",
format(Quality[1:min(length(Quality), features_keep)], digits=5),
")") %>%
paste0(collapse = "\n"))
, by = abs.node.position]
Specifically, this is the code that writes those numbers:
format(Quality[1:min(length(Quality), features_keep)], digits=5)
So, those numbers show the quality of each node, which I think reflects how appropriately that node divides the data. It's been a while since I dealt with these models and I've never been savvy, so I cannot be sure of my interpretation. If you want further explanation about the meaning of quality, you may dig deeper in the source code to figure out how it gets calculated.

For loop using package stratification

I want to write a for loop to get the results of 30 populations.
Those populations were imported by:
nomes<-list.files(pattern=".txt$")
dados<-list()
for (i in 1:length(nomes))
{
dados[[i]]<-read.table(nomes[i])
}
names(dados)<-nomes
So I have 30 populations in one data frame.
I have to find the cv of samples with dalenius hodges methods. Any way, this is not important. The important thing is the loop that I am not doing right. I have tried this:
for (i in nomes)
{
#H=3
dh[[i]] <- strata.cumrootf(x = dados[[i]]$V1, Ls = 3, alloc = c(0.5, 0, 0.5), n=100)
vardh[[i]] <- sum((dh[[i]]$var*dh[[i]]$Nh/(dh[[i]]$Nh-1))*(dh[[i]]$Nh*(dh[[i]]$Nh-dh[[i]]$nh)/dh[[i]]$nh))
cvdh[[i]] <- sqrt(vardh[[i]])/sum(dados[[i]]$V1)
}
But it didnt work. This function is in the "stratification" package.
The error that appear is this:
Error in dh[[i]] <- strata.cumrootf(x = dados[[i]]$V1, Ls = 3, alloc = c(0.5, :
object 'dh' not found
In addition: Warning message:
'nclass' value has been chosen arbitrarily
Can anybody help me?
Thanks.

theta.sparse error with lorDIF

I was wondering whether anyone can help me out.
I am trying to run a dif analysis on my data but keep getting a theta.sparse error, which I am unsure of how to fix. I would really appreciate any that you can give me.
library(lordif)
dat<- read.csv2("OPSO.csv",header=TRUE)
datgender <- read.csv2("DATA.csv",header=TRUE)
group<-datgender$Gender
sink("outputDIFopso.txt")
gender.difopso <- lordif(dat, group, selection = NULL,
criterion = c("Chisqr", "R2", "Beta"),
pseudo.R2 = c("McFadden", "Nagelkerke", "CoxSnell"), alpha = 0.01,
beta.change = 0.1, R2.change = 0.02, maxIter = 10, minCell = 5,
minTheta = -4, maxTheta = 4, inc = 0.1, control = list(), model = "GRM",
anchor = NULL, MonteCarlo = FALSE, nr = 100)
print(gender.difopso)
summary(gender.difopso)
sink()
pdf("graphtestop.pdf")
plot(gender.difopso)
dev.off()
dev.off()
Error in lordif(dat, group, selection = NULL, criterion = c("Chisqr", :
object 'theta.sparse' not found
Thank you :)
You should check the error line before then. The output will probably say you have no items flagged for DIF. When that's the case you should just run the mirt function and extract theta and ipar objects as necessary.
The author could add some case handling for when compare(flags, flags.matrix) is true. At the very least, it seems a warning is omitted when there are no items with DIF the same way it says
if (ndif == ni) {
warning("all items got flagged for DIF - stopping\n")
}
and there is no case handling when (ndif == 0) although compare(flags, flag.matrix) evaluates to TRUE.
The implications when all or none of the items have DIF is that you would get the same results (generating the same ICC plots, same inference etc) by fitting mirt in the combined sample (no DIF) or two or more mirt models for each group (all DIF). So it's a correct time saving procedure to just bypass when all that breaks down.

Hiding output from console

My code contains a command which is essential for it to run, however it ends up showing the result of this command in the console, i have tried to use suppressWarnings(), suppressMessages(), invisible() and sink() but all of these still show the result.
Here is an example data set and where the problem originates from:
M<-c(1111,1222,1333,1444,1555,1666,1777,2223,6654,9867,1123,1456,2436,6875)
fstAdi <- ets(ts(rep(M,length = length(M)), deltat= 1/4, start = c(8,1)), model = "AAA", damped = FALSE, opt.crit = "mae", ic="aic", lower = c(0, 0, 0, 0), upper = c(0.999, 0.999, 0.999, 0.999), bounds = "admissible", restrict = FALSE)
mae11Ad<-summary(fstAdi)[,"MAE"]
The last line of the code above always shows the summary in the console, which when automating this for a report causing problems. Does anyone know of a command which can stop this happening?
Thankyou
I have found a way to hide it while automating the report, but if anyone knows how to hide it while just running the code to make the process quicker then that would be really helpful anyway :)
The summary method for ets objects is a bit verbose:
> forecast:::summary.ets
function (object, ...)
{
print(object)
cat("\nTraining set error measures:\n")
print(accuracy(object))
}
<bytecode: 0x161d31c8>
<environment: namespace:forecast>
This is pretty bad style, summary methods should return an object with a class and the print method for that class should produce the output.
So you could just call the accuracy method on your object:
> accuracy(fstAdi)[,"MAE"]
[1] 1971.468
which has the advantage of not needing any diversion of output and is more readable.
sink does work, how exactly are you using it? Try for example
M<-c(1111,1222,1333,1444,1555,1666,1777,2223,6654,9867,1123,1456,2436,6875)
fstAdi <- ets(ts(rep(M,length = length(M)), deltat= 1/4, start = c(8,1)), model = "AAA", damped = FALSE, opt.crit = "mae", ic="aic", lower = c(0, 0, 0, 0), upper = c(0.999, 0.999, 0.999, 0.999), bounds = "admissible", restrict = FALSE)
sink(tempfile())
mae11Ad<-summary(fstAdi)[,"MAE"]
sink()
I know that probably looks like a bad-style approach, but this seems to work
sink( tempfile() )
mae11Ad<-summary(fstAdi)[,"MAE"]

Resources