[R-br] Implementação de SEM (lavaan) utilizando variáveis dummy [Resolvido]

Vinícius Lionel Mateus vinynegrelli em gmail.com
Terça Abril 21 10:03:07 BRT 2015


Olá useRs,

Estudei mais sobre as dummies, e após algumas buscas na internet 
consegui uma solução para gerá-las "by hand".

# Making dummy variables from a case with K > 2 levels
# Transforming grown to factor
prep$grown.f <- factor(prep$grown, labels = c("1", "2", "3"))
tapply(prep$lnexp_5y, prep$grown.f, mean) # Silly checking of the 
transformation
ido <- sort(unique(prep$grown.f))
contrasts(ido) # Inspecting the binary combinations considering the 
first level as reference
length(ido)
dg <- matrix(NA, nrow = nrow(prep), ncol = (length(ido)-1)) #creating 
empty dummy dataset
dg
d.dim <- (length(ido)-1)
for (j in 1:d.dim) {
   dg[,j] <- as.integer(prep$grown.f == ido[j])
}
dg
prep2 <- cbind(prep, dg)
head(prep2)
prep2$"1" <- as.factor(prep2$"1")
prep2$"2" <- as.factor(prep2$"2")
str(prep2)
names(prep2) <- c("lnexp_5y", "gender", "age",
                      "smoking", "grown", "yearsliving",
                      "children", "hourshome", "job", "odor_binom",
                      "behavioral", "risk_perception",
                      "eyes_a", "nose_a", "run_nose_a", "nausea_a",
                      "cough_a", "chest_a", "breath_a", "fatigue_a",
                      "headache_a", "concentration_a", "dizziness_a",
                      "grown.f", "grown2", "grown3")
prep3 <- prep2
str(prep3)

Se alguém tiver uma solução mais elegante, por favor adicione seu 
comentário. ;)

Abs,
Vinícius

On 04/08/2015 02:58 PM, Vinícius Lionel Mateus wrote:
> Prezados useRs,
>
> Sou um newbie em análise de dados categóricos, e esta é a primeira vez 
> que uso o pacote lavaan.
>
> Ao implementar a análise, recebo o seguinte erro:
>
> *Error in muthen1984(Data = X[[g]], ov.names = ov.names[[g]], ov.types 
> = ov.types,  : **
> **  unknown ov.types:factor*
>
> Acho que estou com problemas de sintaxe, na hora de especificar uma 
> variável dummy, *grown*.
> O código antes do erro é:
>
> #### Making dummy variables from a case with K > 2 levels ###
> # Transforming grown to factor
> raw.data$grown <- factor(raw.data$grown, labels = c("1", "2", "3"))
> class(raw.data$grown)
> str(raw.data) #Inspecting the data set
>
> #the contrast matrix for categorical variable with three levels
> contr.treatment(3)
> #assigning the treatment contrasts to grown
> contrasts(raw.data$grown) = contr.treatment(3)
> str(raw.data) #Inspecting the data set
>
> ### centering and rescaling numeric explanatory variables ###
> prep$lnexp_5y <- scale(prep$lnexp_5y)
> prep$age<-scale (prep$age)
> prep$yearsliving <-scale (prep$yearsliving)
> prep$hourshome <-scale (prep$hourshome)
> str(prep)
>
> # Building the model
> # The observed variable in black is a dummy.
> odor1 <- '
> # latent variables (measurement model)
> rsi =~ eyes_a + nose_a + run_nose_a +
> cough_a + chest_a + breath_a
> nsh =~ dizziness_a + concentration_a +
> headache_a + fatigue_a + nausea_a
> psy =~ risk_perception + behavioral
> ap =~ lnexp_5y
>
>
> sde =~ gender + smoking + *grown* + children + yearsliving
> oa =~ odor_binom
>
> #regressions (dependent ~ independent | y ~ x)
> rsi ~ ap + oa
> psy ~ ap + oa
> nsh ~ ap + oa + psy
> oa ~ ap + sde
> '
> # Fitting the model with WLSMV
> fit1.ml <http://fit1.ml> <- sem(odor1, data = prep, ordered = 
> c("grown"), estimator = "WLSMV")
>
>
> Desde já, muito obrigado.
> Vinícius
> -- 
> Best regards,
>
> Vinícius Lionel Mateus, MSc (http://lattes.cnpq.br/6501001637020665)
> Chemistry PhD Student
> Atmospheric Chemistry Laboratory - Dep. Chemistry
> PUC-Rio - Pontifical Catholic University of Rio de Janeiro
> Rua Marquês de São Vicente, 225, Gávea - Rio de Janeiro, RJ - Brazil CEP: 22451-900
> Telefone:     (+45) 26 28 28 51
>                (+55) (21) 3527-1327
>                (+55) (21) 993 - 588 - 051
> Skype: vinicius.lionel
> http://www.qui.puc-rio.br/index.html

-- 
Best regards,

Vinícius Lionel Mateus, MSc (http://lattes.cnpq.br/6501001637020665)
Chemistry PhD Student
Atmospheric Chemistry Laboratory - Dep. Chemistry
PUC-Rio - Pontifical Catholic University of Rio de Janeiro
Rua Marquês de São Vicente, 225, Gávea - Rio de Janeiro, RJ - Brazil CEP: 22451-900
Telefone:     (+45) 26 28 28 51
               (+55) (21) 3527-1327
               (+55) (21) 993 - 588 - 051
Skype: vinicius.lionel
http://www.qui.puc-rio.br/index.html

-------------- Próxima Parte ----------
Um anexo em HTML foi limpo...
URL: <http://listas.inf.ufpr.br/pipermail/r-br/attachments/20150421/4f30a106/attachment.html>


Mais detalhes sobre a lista de discussão R-br