Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

using hierarchical models with random slope resutling in sampling problem #494

Open
YuanboBQ opened this issue Jul 17, 2024 · 8 comments
Open

Comments

@YuanboBQ
Copy link

Hi, I am currently working with the HSSM package (v 0.2.3) and have encountered an issue while handling my own data, including the "cavanagh_theta" data provided by the package. Specifically, when using hierarchical models with random slope (e.g., stim|participant_id), I am facing the following problem: the sampling process terminates quickly, but the obtained samples are mostly invalid, resulting in all NaN values for r_hat. However, if remove the random slope, the sampling process will be more normal and the results of the sampling will also be acceptable. Could you please investigate this issue? Can you check this issue? Currently, there are still some problems with HSSM when dealing with hierarchical models, so it is still difficult to use it in formal research.

hierarchical_ddm_model_reg_vazt = hssm.HSSM(
        model = "ddm",
        data = cav_data,
        prior_settings="safe",
        loglik_kind = "approx_differentiable", #"analytical"      
        include = [
            {
                "name": "v",
                "formula": "v ~ 0 + stim + (0 + stim|participant_id)",      
                "link": "identity",
            },
            {
                "name": "a",
                "formula": "a ~ 0 + stim + (0 + stim|participant_id)",      
                "link": "identity",
            },
            {
                "name": "z",
                "formula": "z ~ 0 + stim + (0 + stim|participant_id)",      
                "link": "identity",
            },      
            {
                "name": "t",
                "formula": "t ~ 0 + stim + (0 + stim|participant_id)",      
                "link": "identity",
            },                                                 
                     
        ],
    )
    
    print(hierarchical_ddm_model_reg_vazt)

or the following code

# Define a basic hierarchical model with trial-level covariates
hierarchical_ddm_model_reg_vazt = hssm.HSSM(
    model = "ddm",
    data = cav_data,
    loglik_kind = "approx_differentiable", #"analytical"      
    include = [
        {
            "name": "v",
            "formula": "v ~ 1 + stim + (1 + stim|participant_id)",
            "link": "identity",
        },               
        {
            "name": "a",
            "formula": "a ~ 1 + stim + (1 + stim|participant_id)",
            #"formula": "v ~ (1|subj_idx) + theta",
            "link": "identity",
        },  
        {
            "name": "z",
            "formula": "z ~ 1 + stim + (1 + stim|participant_id)",
            "link": "identity",
        }, 
        {
            "name": "t",
            "formula": "t ~ 1 + (1|participant_id)",
            #"formula": "v ~ (1|subj_idx) + theta",
            "link": "identity",
        },                                         
    ],
)

print(hierarchical_ddm_model_reg_vazt)
hierarchical_ddm_model_reg_vazt.sample(
    sampler="nuts_numpyro", 
    chains=2,  # how many chains to run
    cores=4,  # how many cores to use
    draws=1000,  # number of draws from the markov chain
    tune=500,  # number of burn-in samples
    #target_accept = 0.90,
    )
    
@frankmj
Copy link
Collaborator

frankmj commented Jul 17, 2024 via email

@YuanboBQ
Copy link
Author

Hi, Michael
I have try your provided code, and still get the same issue that the sampling process terminates quickly and resulting in all NaN values for r_hat. Here is my revised code

hierarchical_ddm_model_reg_vazt1 = hssm.HSSM(
    model = "ddm",
    data = cav_data,
    prior_settings="safe",
    loglik_kind = "approx_differentiable", #默认为"analytical"      
    include = [
        {
            "name": "v",
            "formula": "v ~ 1 + stim + (1 + stim|participant_id)",
            "prior": {
                "Intercept": {"name": "Normal", "mu": 1, "sigma": 2, "initval": 1},
                "stim": {"name": "Normal", "mu": 0, "sigma": 1}, # "initval": 0
                #"stim|participant_id": {"name": "Normal", "mu": 0, "sigma": {"name": "HalfNormal", "sigma": 0.5}, "initval": 0.5},
                "1|participant_id": {"name": "Normal", "mu": 0, "sigma": {"name": "HalfNormal", "sigma": 1}, "initval": 0}
                },            
            #"link": "identity",
        },    
        {
            "name": "a",
            "formula": "a ~ 1 + stim + (1 + stim|participant_id)",
            "prior": {
                "Intercept": {"name": "Gamma", "mu": 0.5, "sigma": 1.75, "initval": 1},
                "stim": {"name": "Normal", "mu": 0, "sigma": 1},# "initval": 0.1
                #"stim|participant_id": {"name": "Normal", "mu": 0, "sigma": {"name": "HalfNormal", "sigma": 0.5}, "initval": 0.5},
                "1|participant_id": {"name": "Normal", "mu": 0, "sigma": {"name": "HalfNormal", "sigma": 1}, "initval": 0.3}
                },            
            #"link": "identity",
        },                       
                 
    ],
   noncentered = True,   
)

print(hierarchical_ddm_model_reg_vazt1)

hierarchical_ddm_model_reg_vazt1.sample(
                              sampler="nuts_numpyro", 
                              chains=2,  # how many chains to run
                              cores=4,  # how many cores to use
                              draws=1000,  # number of draws from the markov chain
                              tune=500,  # number of burn-in samples
                              target_accept = 0.90,
                              )

@frankmj
Copy link
Collaborator

frankmj commented Jul 17, 2024 via email

@digicosmos86
Copy link
Collaborator

Hi @YuanboBQ,

Can you try adding categorical="stim" to your model definition? Like this:

hierarchical_ddm_model_reg_vazt = hssm.HSSM(
        model = "ddm",
        data = cav_data,
        prior_settings="safe",
        loglik_kind = "approx_differentiable", #"analytical"      
        categorical="stim" # a str or list of all categorical variables
        include = [
            {
                "name": "v",
                "formula": "v ~ 0 + stim + (0 + stim|participant_id)",      
                "link": "identity",
            },
            {
                "name": "a",
                "formula": "a ~ 0 + stim + (0 + stim|participant_id)",      
                "link": "identity",
            },
            {
                "name": "z",
                "formula": "z ~ 0 + stim + (0 + stim|participant_id)",      
                "link": "identity",
            },      
            {
                "name": "t",
                "formula": "t ~ 0 + stim + (0 + stim|participant_id)",      
                "link": "identity",
            },                                                 
        ],
    )

@AlexanderFengler
Copy link
Collaborator

Hi @YuanboBQ ,
If you use a regression formulation as v ~ 1 + (1 + C(stim)|participant_id) do you run into the same issue?

@YuanboBQ
Copy link
Author

Hi, @frankmj and @digicosmos86, I have tried to add categorical="stim" or use the regression formulation as v ~ 1 + (1 + C(stim)|participant_id) or combine both of the two, and still got the same issue.

@AlexanderFengler
Copy link
Collaborator

@YuanboBQ ,
Could you take the regression out of the t parameter?
t is providing a bit of a pain point thus far, might be the actual culprit here too :/.

@YuanboBQ
Copy link
Author

Hi, @AlexanderFengler, I did not include the regression of the t parameter. Here is my code

hierarchical_ddm_model_reg_vazt = hssm.HSSM(
    model = "ddm",
    data = cav_data,
    prior_settings="safe",
    loglik_kind = "approx_differentiable", #默认为"analytical"  
    #categorical="stim",    
    include = [
        {
            "name": "v",
            "formula": "v ~ 1 + stim + (1 + C(stim)|participant_id)",
            "prior": {
                "Intercept": {"name": "Normal", "mu": 1, "sigma": 2, "initval": 1},
                "stim": {"name": "Normal", "mu": 0, "sigma": 1}, # "initval": 0
                #"stim|participant_id": {"name": "Normal", "mu": 0, "sigma": {"name": "HalfNormal", "sigma": 0.5}, "initval": 0.5},
                "1|participant_id": {"name": "Normal", "mu": 0, "sigma": {"name": "HalfNormal", "sigma": 1}, "initval": 0}
                },            
            "link": "identity",
        },    
        {
            "name": "a",
            "formula": "a ~ 1 + stim + (1 + C(stim)|participant_id)",
            "prior": {
                "Intercept": {"name": "Gamma", "mu": 0.5, "sigma": 1.75, "initval": 1},
                "stim": {"name": "Normal", "mu": 0, "sigma": 1},# "initval": 0.1
                #"stim|participant_id": {"name": "Normal", "mu": 0, "sigma": {"name": "HalfNormal", "sigma": 0.5}, "initval": 0.5},
                "1|participant_id": {"name": "Normal", "mu": 0, "sigma": {"name": "HalfNormal", "sigma": 1}, "initval": 0.3}
                },            
            "link": "identity",
        },                       
                 
    ],
   #noncentered = True,   
)

print(hierarchical_ddm_model_reg_vazt)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants