SydneyBioX / spicyR

https://sydneybiox.github.io/spicyR/
8 stars 4 forks source link

No coefficient column in my results #42

Closed edridgedsouza closed 3 months ago

edridgedsouza commented 3 months ago

When I run spicy on my data, the functions will finish running, but any downstream functions will break because the resulting objects don't have any coefficients. Inspecting the objects, I see NA values in the data frame which has only an (Intercept) column, and NA as well in the p.values slot. I'm not sure how to reproduce this fully, but I obtained the x and y locations from seurat's GetTissueCoordinates() function.

This is what I ran:

coord_df <- bind_rows(lapply(all_objects, function(x) x@meta.data)) %>% 
    select(samplename, cellType, imagerow, imagecol, Treatment_Arm) %>%
    rename(imagerow='x', imagecol='y', samplename='imageID') %>%
    mutate(cellID = row_number())

# # Credit to https://github.com/SydneyBioX/spicyR/issues/40
pheno <- patient_metadata[, c('samplename','Treatment_Arm', 'EGFR_Status', 
                          'Percent_Tumor_PDL1_Expression', 
                          'Path_Response_Classification', 'Recurrence')] %>%
             rename(samplename='imageID')
coord.sc <- SegmentedCells(
    cellData = as.data.frame(coord_df),
    spatialCoords = c("x", "y"),
    cellTypeString = "cellType",
    cellIDString = "cellID",
    imageIDString = "imageID"
  )
imagePheno(coord.sc) <- pheno

# Run the actual tests
spicyTest <- spicy(
  coord.sc,
  condition = "Treatment_Arm",
  subject = "imageID",
  cellTypeCol = "cellType"
)

spicyTestPair <- spicy(
  coord.sc,
  condition = "Treatment_Arm",
  subject = "imageID",
  from = "Malignant",
  to = "T.cells"
)

My first instinct was to think that I simply have no significant results, but I then even if that were the case, I would still be able to see a large value in the p.value slot. So I'm wondering if there's a way to fix my input so that I don't keep getting these NAs and missing coefficient columns. Tracing back through the source I think it has something to do with this block of code but I'm not sure how exactly I can prevent or fix it.

edridgedsouza commented 3 months ago

Diagnosed the issue here. The NA values were caused because of my experimental design, where each image was from a different sample. In the mixed model, the + (1|subject) term naturally breaks the model in a way that isn't the case for the simple linear model. A simple fix would be to display an error/warning message when running the lmer test so the user is aware their input experimental design is incompatible with a mixed model, and that the regular lm can be invoked by omitting the subject parameter in spicy().

alexrunqin commented 3 months ago

Hi Edridge, Thanks for pointing this out! The spicy() function has now been updated to reflect these recommended changes, and converts the mixed model to a linear model when there's a 1-to-1 mapping between the images and the subjects.