Adding a new variable to the dataset?

Everything related to the development of modules in jamovi
Post Reply
sfcheung
Posts: 11
Joined: Wed Jan 19, 2022 5:29 am

Adding a new variable to the dataset?

Post by sfcheung »

Please pardon me for asking a naive question. I believe this is a basic task in writing module. However, I tried searching the forum but my search was rejected, saying that my search terms are too common.

When writing a module, how can we add new variables to the data set, like Linear Regression that can save Cook's distance and residuals? I have read the tutorial in https://dev.jamovi.org/ and also successfully wrote a simple package. However, I can't recall coming across a page on how to a variable. Maybe it is there but I missed it?

-- Shu Fai
User avatar
jonathon
Posts: 2620
Joined: Fri Jan 27, 2017 10:04 am

Re: Adding a new variable to the dataset?

Post by jonathon »

hi,

yes, this is something i need to document. your best bet is to see how jmv does it. here's how we do residuals in the ANOVA:

https://github.com/jamovi/jmv/blob/master/jamovi/ancova.r.yaml#L228-L238

and then you could populate it with something like:

https://github.com/jamovi/jmv/blob/master/R/ancova.b.R#L643-L646

cheers

jonathon
sfcheung
Posts: 11
Joined: Wed Jan 19, 2022 5:29 am

Re: Adding a new variable to the dataset?

Post by sfcheung »

Thanks a lot for your prompt reply! I will take a look and learn how it works.

-- Shu Fai
sfcheung
Posts: 11
Joined: Wed Jan 19, 2022 5:29 am

Re: Adding a new variable to the dataset?

Post by sfcheung »

I tried to understand how to add variables but failed, for now.

This is what I added in *.r.yaml:

Code: Select all

    - name: hv
      type: Output
      title: Leverage
      varTitle: Leverage
      varDescription: Leverage
      measureType: continuous
      clearWith:
        - dv
        - iv
This is what I tried in the .run function, used "0s" just for testing:

Code: Select all

if (self$results$hv$isNotFilled()) {
  self$results$hv$setRowNums(rownames(self$data))
  self$results$hv$setValues(rep(0, nrow(self$data)))
}
No new variable was created, and there was no error message. I also tried to generate something as in this line, with the values computed inside setValues(...), but also failed:

https://github.com/jamovi/jmv/blob/addb ... .b.R#L1090

ANOVA in jmv uses active binding and private members, and they are used in setValues(). Are these elements required to make the "Output" type result add new output variables, and this type won't work if the values to be added are not stored in a member of "self"?

-- Shu Fai
User avatar
jonathon
Posts: 2620
Joined: Fri Jan 27, 2017 10:04 am

Re: Adding a new variable to the dataset?

Post by jonathon »

could you commit this project somewhere, and point me to the repo? then i can take a proper look.

cheers

jonathon
sfcheung
Posts: 11
Joined: Wed Jan 19, 2022 5:29 am

Re: Adding a new variable to the dataset?

Post by sfcheung »

This is the test repo:

https://github.com/sfcheung/jmvtest

This is how the output is set up:

https://github.com/sfcheung/jmvtest/blo ... ml#L33-L41

This is my attempt to save the variable:

https://github.com/sfcheung/jmvtest/blo ... .R#L41-L45

Thanks a lot.

-- Shu Fai
User avatar
jonathon
Posts: 2620
Joined: Fri Jan 27, 2017 10:04 am

Re: Adding a new variable to the dataset?

Post by jonathon »

sorry, i forgot to mention ... you need a matching Output option in your .a.yaml too:

https://github.com/jamovi/jmv/blob/master/jamovi/ancova.a.yaml#L405-L407

jonathon
sfcheung
Posts: 11
Joined: Wed Jan 19, 2022 5:29 am

Re: Adding a new variable to the dataset?

Post by sfcheung »

I did not notice that there must be a check box for it to work. It works now! Many many thanks!

-- Shu Fai
Post Reply