0
$\begingroup$

I try to run this part of my code

# Calculate spearman correlation test between the ordinal dependent variable and other variables
spearman_corr <- map_dbl(other_vars, function(x) cor(dataset[[x]], dataset[[ordinal_dep_var]], method = "spearman"))

and got this error,

Error in `vectbl_as_col_location2()`:

! Can't extract column with ordinal_dep_var. ✖ Subscript ordinal_dep_var must be size 1, not 1000.

I don't know what I am doing wrong. The Dependent variable is a column with levels (Low, Medium, High)

The purpose is to later run this to get the significant ones

# Select significant variables based on the spearman correlation test
significant_vars <- other_vars[abs(spearman_corr) > 0.5]

then use that and create an ordinal logistics regression to predict the level

here's a link to my dataset if needed cancer dataset

and here's my full code snippet

enter image description here

Thanks

$\endgroup$
2
  • $\begingroup$ You likely need dataset[["ordinal_dep_var"]] (note the quotes) instead. $\endgroup$ Commented Dec 31, 2022 at 19:27
  • $\begingroup$ @DaveArmstrong Yeah that didn't work instead gave this error "Error in cor(dataset[[x]], dataset[["ordinal_dep_var"]], method = "spearman") : supply both 'x' and 'y' or a matrix-like 'x'" $\endgroup$
    – Philipe
    Commented Dec 31, 2022 at 19:30

1 Answer 1

0
$\begingroup$

This should work:

dataset <- rio::import("~/Downloads/cancer patient data sets (1) (1).xlsx")
dataset$ordinal_dep_var <- factor(dataset$Level, levels=c("Low", "Medium", "High"))
other_vars <- setdiff(colnames(dataset), c("ordinal_dep_var", "Level", "Patient Id"))
spearman_corr <- purrr::map_dbl(other_vars, function(x) cor(dataset[[x]], as.numeric(dataset[["ordinal_dep_var"]]), method = "spearman"))
spearman_corr
#>  [1]  0.07939082 -0.16486688  0.62146621  0.68179201  0.70278907  0.66097744
#>  [7]  0.67560519  0.61380590  0.69247738  0.81536376  0.48428460  0.68314269
#> [13]  0.65498263  0.76631837  0.63116882  0.35430186  0.48394304  0.20381774
#> [19]  0.23153439  0.31123246  0.45911148  0.35320136  0.28226940

Note, that you need to remove the Patient Id and Level variables (as they are not numeric) and turn the ordinal_dep_var into numeric. You actually don't need map_dbl, you can do it all with cor():

spearman_corr <- cor(dataset[,other_vars], as.numeric(dataset$ordinal_dep_var), method="spearman")
spearman_corr
#>                                 [,1]
#> Age                       0.07939082
#> Gender                   -0.16486688
#> Air Pollution             0.62146621
#> Alcohol use               0.68179201
#> Dust Allergy              0.70278907
#> OccuPational Hazards      0.66097744
#> Genetic Risk              0.67560519
#> chronic Lung Disease      0.61380590
#> Balanced Diet             0.69247738
#> Obesity                   0.81536376
#> Smoking                   0.48428460
#> Passive Smoker            0.68314269
#> Chest Pain                0.65498263
#> Coughing of Blood         0.76631837
#> Fatigue                   0.63116882
#> Weight Loss               0.35430186
#> Shortness of Breath       0.48394304
#> Wheezing                  0.20381774
#> Swallowing Difficulty     0.23153439
#> Clubbing of Finger Nails  0.31123246
#> Frequent Cold             0.45911148
#> Dry Cough                 0.35320136
#> Snoring                   0.28226940

Created on 2022-12-31 by the reprex package (v2.0.1)

$\endgroup$
1
  • $\begingroup$ This worked perfectly thanks alot. $\endgroup$
    – Philipe
    Commented Dec 31, 2022 at 19:40

Not the answer you're looking for? Browse other questions tagged or ask your own question.