0

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
rrh
  • 9
  • 2
  • `dlogis()` is going to give you a value for every number you put it. So, for example, if you feed it a length 23 vector, it will evaluate the logistic p.d.f. for each of those 23 elements. `optim()` assumes you are optimizing a *single-valued* function. Maybe you're wanting to use something like `f <- function(x) sum(log(dlogis(x)))` and feed `f` to `optim()`? Kind of hard to understand what your end goal is. – duckmayr Mar 12 '20 at 21:37
  • I see. thank you!!!! :) I will try it! – rrh Mar 12 '20 at 22:00

0 Answers0