not recognizing data as numeric

Hello everyone,

I need some help with this. I tried different ways but can't get this code to work...the issue is with making R recognize the data as numeric (maybe). I am doing a mediation analysis using survey data ( and the data i have has only numeric variables. I imported it from excel and the code works until it tells me that the outcome variable is not numeric...when it is numeric. I tried different ways of converting it in R (apply function, `data <- apply(X, 2, as.numeric)' , but still not working. Code is below. Any suggestions on how to proceed ? Thank you !

> library(readxl)
> data <- read_excel("C:/path/data.xlsx", 
    col_types = c("numeric", "numeric", "numeric", 
         "numeric", "numeric"))
> View(data)
tibble [11,926 × 5] (S3: tbl_df/tbl/data.frame)
 $ zfrs    : num [1:11926] -0.8081 -0.8953 -0.3871 -0.5597 0.0167 ...
 $ zcvd    : num [1:11926] -0.138 -0.138 -0.138 -0.138 -0.138 ...
 $ zcesd10 : num [1:11926] -1.2 -1.2 -1.2 -1.2 -1.2 ...
 $ mwgtname: num [1:11926] 0.547 1.281 -0.415 -0.742 -0.641 ...
 $ zsdoh   : num [1:11926] 0.672 -2.495 0.144 -0.384 1.2 ...


 R <- 20
> wgtnames <- paste("repwgt", seq(0,R,by=1), sep="")
> mwgtname=wgtnames[1]
> repwgtnames=wgtnames[2:(R+1)]
> model1 <- ' # outcome
              zcvd ~ u2*1 + c*zcesd10 + b*zfrs
            # mediator
             zfrs ~ u1*1 + a*zcesd10
            # indirect effect (a*b)
              ab := a*b
            # total effect
              total := c + (a*b)
> fit.BRR <-, data=data, mwgtname=mwgtname,
          repwgtnames=repwgtnames, fayfactor=0.5)

*Error in = model1, data = data, mwgtname = mwgtname,  : *
*  value of zcvdis not numerical*

> is.numeric(data$zcvd)
[1] TRUE
> is.numeric(data)

*I transform the data into numeric:* 

data1 <- apply(data, 2, as.numeric)'
[1] TRUE

*then run the model again:* 
fit.BRR <-, data=sol1, mwgtname=mwgtname,
                       repwgtnames=repwgtnames, fayfactor=0.5)
*Error in data[, mwgtname] : subscript out of bounds*
*not sure how to address this as the number of columns did not change...*

why did you choose 20 here ?
you have only 5 columns total, so its not possible for you to have 20 replicate weight columns

got it, thank you. I switch it to 5 and repeated the analysis, but unfortunately still same error :frowning:

you dont have 5 replicate weights either... seems like you have a main weight and 4 other variables that are involved in your model1. do you have replicate weights at all ?

my advise is to slow down. look at examples of the use of in the documentation.
compare the data layout to yours, understand why the example has what it has, and find the analogies between the example and your own setup.

thanks, appreciate the response . yes I do have replicate weights (created the svydesign then got the replicate weights using the jackknife delete n method I had a stratified designs with an odd number of clusters). But the problem that persists even when using the replicate weights is the fact that R does not recognizes data as numeric even though it's numeric regardless of what weights I use...
*Error in = model1, data = sol, mwgtname = mwgtname, : *

  • value of Zcvdis not numerical*

appreciate any further suggestions!

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.