Constraining a covariate-path in lavaan - r

I have the following lavaan model:
model <- ' i =~ 1*t1 + 1*t2 + 1*t3 + 1*t4 + 1*t5 + 1*t6 + 1*t7 + 1*t8 + 1*t9 + 1*t10 + 1*t11 + 1*t12 + 1*t13+ 1*t14 + 1*t15 + 1*t16 + 1*t17 + 1*t18 + 1*t19 + 1*t20
s =~ 0*t1 + 1*t2 + 2*t3 + 3*t4 + 4*t5 + 5*t6 + 6*t7 + 7*t8 + 8*t9 + 9*t10 + 10*t11 + 11*t12 + 12*t13 + 13*t14 + 14*t15 + 15*t16 + 16*t17 + 17*t18 + 18*t19 + 19*t20
t8 ~~ 0.01*t8
t17 ~~ 0.01*t17
t18 ~~ 0.01*t18
# regressions
s ~ h_index
i ~ h_index'
fit_UNconstrained <- growth(model, data=growth_data, group = "type")
summary(fit_UNconstrained)
Now, I would like to create a model that constraints the paths s ~ h_index and i ~ h_index to be equal across all groups ("type"). How can I accomplish this?

I believe this works in the same way as it would for adding group-wise constraints to the indicators of a latent factor. And if that's the case, then all you need to do is to add a vector of labels next to the predictor you want to constrain across groups. In your case, you have two parameter estimates you want to constrain, so you'd add two vectors.
The length of the vector is going to depend on the number of groups you have, and the label will be the same for all groups.
Let's suppose you have three groups; then your code would look something like the following.
model <- "
i =~ 1*t1 + 1*t2 + 1*t3 + 1*t4 + 1*t5 + 1*t6 + 1*t7 + 1*t8 + 1*t9 + 1*t10 + 1*t11 + 1*t12 + 1*t13+ 1*t14 + 1*t15 + 1*t16 + 1*t17 + 1*t18 + 1*t19 + 1*t20
s =~ 0*t1 + 1*t2 + 2*t3 + 3*t4 + 4*t5 + 5*t6 + 6*t7 + 7*t8 + 8*t9 + 9*t10 + 10*t11 + 11*t12 + 12*t13 + 13*t14 + 14*t15 + 15*t16 + 16*t17 + 17*t18 + 18*t19 + 19*t20
t8 ~~ 0.01*t8
t17 ~~ 0.01*t17
t18 ~~ 0.01*t18
# regressions
s ~ c(v1, v1, v1)*h_index
i ~ c(v2, v2, v2)*h_index
"
fit_UNconstrained <- growth(model, data=growth_data, group = "type")
summary(fit_UNconstrained)
Here the vectors c(v1, v1, v1) and c(v2, v2, v2) are telling lavaan to constrain these parameter estimates to be equal across groups.
I believe this should do what you have in mind.

Related

CFA lavaan (R) negative variance: How can I test which variance is negative

I'm doing a cfa with lavaan and I geht the warning: some estimated ov variances are negative.
My model contains 8 factors and 1 second order factor:
FEMusiK.model <- 'Unterhaltung =~ FEMusiK1 + FEMusiK2 + FEMusiK3 + FEMusiK4
Erholung =~ FEMusiK5 + FEMusiK6 + FEMusiK7 + FEMusiK8 + FEMusiK9 + FEMusiK10 + FEMusiK11
sEmpfindungen =~ FEMusiK12 + FEMusiK13 + FEMusiK14 + FEMusiK15 + FEMusiK16 + FEMusiK17 + FEMusiK18
Ablenkung =~ FEMusiK19 + FEMusiK20 + FEMusiK21 + FEMusiK22 + FEMusiK23
Entladung =~ FEMusiK24 + FEMusiK25 + FEMusiK26 + FEMusiK27 + FEMusiK28 + FEMusiK29
mArbeit =~ FEMusiK30 + FEMusiK31 + FEMusiK32 + FEMusiK33 + FEMusiK34
Trost =~ FEMusiK35 + FEMusiK36 + FEMusiK37 + FEMusiK38 + FEMusiK39 + FEMusiK40
Immersion =~ FEMusiK41 + FEMusiK42 + FEMusiK43 + FEMusiK44 + FEMusiK45
FEMusiK_global =~ Unterhaltung + Erholung + sEmpfindungen + Ablenkung + Entladung + mArbeit + Trost + Immersion'
FEMusiK.model.fit <- cfa(FEMusiK.model, data=Data, estimator="MLR", missing= "FIML")
summary(FEMusiK.model.fit, fit.measures=TRUE, standardized=TRUE)
My sample size is 78 and I did MLR as estimator because the data is non normal distributed.
How can I test, which variance is negative and how can I fix the problem?
Thank you for your help!

The leading minor of order 4 is not positive definite in npplreg

I am running the following code:
mydata1 = data.frame(dataset)
mydata1 <- na.omit(mydata1)
bw <- npplregbw(mydata1$X1 ~ mydata1$X2 + mydata1$X3 + mydata1$X4 + mydata1$effect_1993 + mydata1$effect_1994 + mydata1$effect_1995 + mydata1$effect_1996 + mydata1$effect_1997 + mydata1$country_2 + mydata1$country_3 + mydata1$country_4 + mydata1$country_5 + mydata1$country_6 + mydata1$effect_1998 + mydata1$effect_1999 + mydata1$effect_2000 + mydata1$effect_2001 + mydata1$effect_2002 + mydata1$effect_2003 + mydata1$effect_2004 + mydata1$effect_2005 + mydata1$effect_2006 + mydata1$effect_2007 + mydata1$effect_2008 + mydata1$effect_2009 + mydata1$effect_2010 + mydata1$effect_2011 + mydata1$effect_2012 + mydata1$effect_2013 + mydata1$effect_2014 + mydata1$effect_2015 + mydata1$effect_2016 + mydata1$effect_2017 + mydata1$effect_2018 + mydata1$effect_2019 + mydata1$effect_2020 + mydata1$effect_2021|mydata1$X5 + mydata1$X6 + mydata1$X7 + mydata1$X8, data = mydata1, na.action = na.omit)
summary(bw)
reg_np <- npplreg(bw)
The code is running fine except the last command which gives the following error:
Error in chol.default(t(model.matrix(model)) %*% model.matrix(model)) :
the leading minor of order 4 is not positive definite
My data do not have 0 (except the fixed effects data) or NA values.
Is there any way I can proceed with the npplreg regression without getting that error?
Thanks a lot in advance

Number of Observations in Regression

How can i find out how many observations were used in a regression?
model_simple <- as.formula("completion_yesno ~ ac + ov + UCRate + FirstWeek + LastWeek + DayofWeekSu + DayofWeekMo + DayofWeekTu + DayofWeekWe + DayofWeekTh + DayofWeekFr + MonthofYearJan + MonthofYearFeb + MonthofYearMar + MonthofYearApr +MonthofYearMay+ MonthofYearJun + MonthofYearJul + MonthofYearAug + MonthofYearSep + MonthofYearOct + MonthofYearNov")
clog_simple1 = glm(model_simple,data=cllw,family = binomial(link = cloglog))
summary(clog_simple1)
I have tried the fitted command which did not result in a concrete number of observations N
Use the built in nobs() function
nobs(clog_simple1)

How to change the polynomial order in a for-loop using the poly R function?

I would like to fit a regression by trying different polynomials, and I tried running this loop:
for (p_order in 1:9) {
assign(paste("RD0", p_order, sep = ""), electricity_price ~ d1 + gas_price + coal_price +
oil_price + EUA + weekday + month + median_windspeed1 +
median_windspeed2 + median_windspeed3 + median_windspeed4 +
sun1 + sun2 + sun3 + sun4 + median_temp1 + median_temp2 +
median_temp3 + median_temp4 + poly(as.numeric(date), p_order, raw=TRUE) + time)
}
Although it creates correctly the names of the variables (RD01, RD02, etc), instead of saving the correct order of the polynomial (1,2, etc) it stores "p_order". For example,
> RD04
electricity_price ~ d1 + gas_price + coal_price + oil_price +
EUA + weekday + month + median_windspeed1 + median_windspeed2 +
median_windspeed3 + median_windspeed4 + sun1 + sun2 + sun3 +
sun4 + median_temp1 + median_temp2 + median_temp3 + median_temp4 +
poly(as.numeric(date), p_order, raw = TRUE) + time
> RD07
electricity_price ~ d1 + gas_price + coal_price + oil_price +
EUA + weekday + month + median_windspeed1 + median_windspeed2 +
median_windspeed3 + median_windspeed4 + sun1 + sun2 + sun3 +
sun4 + median_temp1 + median_temp2 + median_temp3 + median_temp4 +
poly(as.numeric(date), p_order, raw = TRUE) + time
Could someone explain me why and how to sort this out?
Thank you!
Create the formula first, then assign it. I will post a simplified example.
for (p_order in 1:2) {
fmla <- paste("electricity_price ~ d1 + poly(as.numeric(date),", p_order, ", raw = TRUE)")
assign(paste("RD0", p_order, sep = ""), as.formula(fmla))
}
RD01
#electricity_price ~ d1 + poly(as.numeric(date), 1, raw = TRUE)
RD02
#electricity_price ~ d1 + poly(as.numeric(date), 2, raw = TRUE)

Error in First Differences Model

I am very new to R and trying to tackle some homework that is giving me trouble. I think I have just about everything worked out, except last glitch.
When I create the following First differences models (using my two panel datasets):
out00 <- plm(logmrate ~ 0 + lawchange + logbeertaxa + y70 + y71 + y72 + y73 + y74 + y75 + y76 + y77 + y78 + y79 + y80 + y81 + y82 + y83 + y84 + y85 + y86 + y87 + y88 + y89 + y90 + y91 + y92 + y93 + y94 + y95 + y96, data = pdt.deaths, model = 'fd')
out01 <- plm(logmrate ~ 0 + lawchange + logbeertaxa + y70 + y71 + y72 + y73 + y74 + y75 + y76 + y77 + y78 + y79 + y80 + y81 + y82 + y83 + y84 + y85 + y86 + y87 + y88 + y89 + y90 + y91 + y92 + y93 + y94 + y95 + y96, data = pdt.deaths1, model = 'fd')
stargazer(out00, type="text")
stargazer(out01, type="text")
I get this error term returned for both models:
Error in crossprod(t(X), beta) : non-conformable arguments
The variable "lawchange" is a 1 or 0 variable, and each of the year variables ("y70"..."y96") are year indicator variables to account for time

Resources