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 (med.fit.BRR) 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)
str(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 ...
library(MedSurvey)
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 <- med.fit.BRR(model=model1, data=data, mwgtname=mwgtname,
repwgtnames=repwgtnames, fayfactor=0.5)
*Error in med.fit.BRR(model = model1, data = data, mwgtname = mwgtname, : *
* value of zcvdis not numerical*
> is.numeric(data$zcvd)
[1] TRUE
> is.numeric(data)
[1] FALSE
*I transform the data into numeric:*
data1 <- apply(data, 2, as.numeric)'
is.numeric(sol1)
[1] TRUE
*then run the model again:*
fit.BRR <- med.fit.BRR(model=model1, 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...*