0

Is it possible to store variables in R..

I have to compute 10*324 fitdist's which each output from this function has to be stored inside a matrix with the size above? is that possible in R?

If so then how, i am completely lost?

So i tried creating a simple example

norm_dist <- as.data.frame(matrix(nrow=3,ncol=3))
data(iris)
for(i in 1:3)
{
  for(j in 1:3)
  {
    print(i)
    print(j)
    if(j==1)
    {
      element = fitdist(data =iris$Petal.Width[1:50*i], distr = "norm")
      norm_dist[i,j] = element
    } 
    if(j==2)
    {
      element = fitdist(data =iris$Petal.Length[1:50*i], distr = "norm")
      norm_dist[i,j] = element
    } 
   if(j==3)
    {
      element = fitdist(data =iris$Sepal.Length[1:50*i], distr = "norm")
      norm_dist[i,j] = element
    } 
  }

}

But i am getting this error

Error in `[<-.data.frame`(`*tmp*`, i, j, value = list(estimate = c(0.867771222640304,  : 
  replacement element 4 is a  matrix with 2 rows, needs 1 

I am not sure i understand what it means...

Lamda
  • 914
  • 3
  • 13
  • 39

1 Answers1

1

you might want to check out the ?fitdist documentation under "Value" section. It mentions the output from the function which is a list with a few components.

Which of those value do you want to assign into norm_dist? For example, if you want the log-likelihood, you can use norm_dist[i,j] = element$loglik

If you want to store the whole object to be stored, you will need a list rather than a data.frame, for e.g.

norm_dist_res <- list()
for(i in 1:10)
{
    for(j in 1:324)
    {
        norm_dist_res[[paste0(i,"-",j)]] <- fitdist(data=g_all_p$data[1:8000*i, j], distr="norm")
    }
}
chinsoon12
  • 25,005
  • 4
  • 25
  • 35
  • I am getting this error `Error in `[[<-.data.frame`(`*tmp*`, paste0(i, "-", j), value = list(estimate = c(0.867771222640304, : replacement has 17 rækker, data has 10` – Lamda Apr 19 '16 at 06:53
  • How do i extract one single value? – Lamda Apr 19 '16 at 06:58
  • 2-1 and 3-2 has the precise same input – Lamda Apr 19 '16 at 07:02
  • 1
    you will need to check your indexing in `g_all_p$data[1:8000*i, j]`. I dont quite understand how you are accessing your data vector. in essence, i dont know what `g_all_p$data` is. – chinsoon12 Apr 19 '16 at 07:11
  • g_all_p$data is a matrix with 80000 rows and 324 cols Each 8000 belong to one class, these are observations made for one class and each row for these 8000 rows are the features extracted. I am trying to fit each vector to a norm dist. and save that variable in a matrix like structure. – Lamda Apr 19 '16 at 07:16
  • The output matrix is suppose to have 10 rows and 324 cols. – Lamda Apr 19 '16 at 07:17
  • If you want to store the whole object to be stored, you will need a list rather than a data.frame – chinsoon12 Apr 19 '16 at 07:25
  • Ohh.. wait i wrote my for loop incorrectly.. I have to change the row of the matrix i am searching in.. The first 8000 rows belong to class 0 and 8000 - 16000 belong to class 1 and so on.. the step size has to be changed in the for loop you have written. – Lamda Apr 19 '16 at 07:26
  • I seem to messing things up, What i am doing wrong? http://pastebin.com/fFSv42pP – Lamda Apr 19 '16 at 08:55
  • try `fitdist(data=g_all_p$data[x:y], distr="norm")`. you might want to run `1+(8000*i-1)):8000*i` to understand operator precedence – chinsoon12 Apr 19 '16 at 09:12