Modelling change over time in mediation model

Hey! I am new in the R-world and I am trying to model the change in Z due to X in a longitudinal mediation model. I developed a function to describe the change in Z but keep getting an error when I try to plot zchange. "t" is supposed to be the time that has passed, I suppose there is something wrong with the way I use that? Can anyone tell me what is wrong with my code?
Thank you in advance! :slight_smile:

zchange <- function(a=0.5, t, T=1) {if (t<=0){0}
else if ((0<t)&(t<T)){at}
else if (t>=T) {a
T}}

Hi, and welcome!

Please see the FAQ: What's a reproducible example (`reprex`) and how do I create one? Using a reprex, complete with representative data will attract quicker and more answers.

I've reinterpreted the function (for one reason because there's no adjacency operator for multiplication and some of the tests were unneeded)

library(purrr)

zchange <- function(t, a = 0.5, T = 1) {
    ifelse(t <= 0, 0, 
        ifelse(t < T, a*t, a*T)
        )
    }

t <- seq(from = 0.01, to = 0.49, by = 0.01)
z <- map(t, zchange) %>% unlist()
plot(t,z)

Created on 2020-03-07 by the reprex package (v0.3.0)

It would be more interesting, of course, with varying a and T. BTW: I put t as the first argument, because R uses positional processing and a and T have defaults,

Ahh thank you so much!

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.