How to calculate "AIC" in a svyglm model? - r

I have this model and I need to calculate "AIC" to make model selection, my problem is that when I run the model "AIC" appears as "NA".
MODEL= svyglm(formula = Asiste ~ afro + sexo + E27 + JovenActivo + hijos +
jefe + LN_YSVL_sin_joven_prom + aniosed + climaeducativo +
Mdeo + icv2 + TV + Computadora + Telefono + Internet + Cable +
Calefon + DVD + Microhondas + Aire + Auto_o_moto + Secadora +
Madre_ausente + Internet + TV + Lavavajillas + Refrigerador +
climaeducativo + Actividad_del_Jefe + Hacinamiento, family = quasibinomial(link =
"logit"),
data = Personas.con.muestra, design = diseƱo_personas_14_17,
subset = (Stratum != 0))
MODEL$aic
[1] NA
Any suggestion?
Thanks!! Natalia

You can try this:
summary(MODEL)$aic

Related

Bandwidth Selection on Semi parametric Regression: npplregbw error

I am trying to find the bandwidth of a semiparametric panel regression of the form:
x1it = a1 + a2 x2it + a3 x3it + a4 x4it + fei + fet + g(x5it) + uit
where fei and fet are the country and time fixed effects. For that I am using the code:
bw <- npplregbw(x1 ~ x2+ x3+ x4+ effect1 + effect2 + effect3 + effect4 + effect5 + effect6 + effect7 + effect8 + effect9 + effect10 + effect11 + effect12 + effect13 + effect14 + effect15 + effect16 + effect17 + effect18 + effect19|x5, data = mydata, na.action = na.omit)
But when I am running that I am getting the following error:
Error in npregbw.rbandwidth(xdat = xdat, ydat = ydat, bws = tbw, bandwidth.compute = bandwidth.compute) :
number of regression data and response data do not match
I cannot understand what is the problem.
Any help would be appreciated.
Thanks a lot

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)

create for loop in R to run same model for 10 times

I am trying to write a loop in R after imputation. The imputation imputed 10 data sets and I want to run the same model in each data set and predict score of outcome in each data set. My current code does not work:
for (i in 1:10) {
impi <- glm(died ~ agecat + female_1 + insurance + mech + transfer +
iss + mxaisbr1 + maxais + cm_chf_1 + cm_mets_1 + cm_liver_1 +
cm_htn_c_1 + cm_bldloss_1 + state, data = subset(imp, .imp == i), family = binomial)
preimpi <- predict(impi, type = c('response'))
}
Without the loop, I have to type the same code for 10 times
imp1 <- glm(died ~ agecat + female_1 + insurance + mech + transfer +
iss + mxaisbr1 + maxais + cm_chf_1 + cm_mets_1 + cm_liver_1 +
cm_htn_c_1 + cm_bldloss_1 + state, data = subset(imp, .imp == 1), family = binomial)
preimp1 <- prediect(imp1, type = c('response'))
imp2 <- glm(died ~ agecat + female_1 + insurance + mech + transfer +
iss + mxaisbr1 + maxais + cm_chf_1 + cm_mets_1 + cm_liver_1 +
cm_htn_c_1 + cm_bldloss_1 + state, data = subset(imp, .imp == 2), family = binomial)
preimp2 <- prediect(imp2, type = c('response'))
etc...
Any idea? Thanks!
One solution is to initialize impi and preimpi as lists, e.g.,
impi = list()
preimpi = list()
for (i in 1:10) {
impi[[i]] <- glm(died ~ agecat + female_1 + insurance + mech +
transfer + iss + mxaisbr1 + maxais + cm_chf_1 +
cm_mets_1 + cm_liver_1 + cm_htn_c_1 + cm_bldloss_1 +
state, data = subset(imp, .imp == i), family = binomial)
preimpi[[i]] <- predict(impi[[i]], type = c('response'))
}
Note the use of double brackets for indexing a list.
Edit: Here is an alternative that uses lapply for the predict line, which should produce the same output as the example above.
impi = list()
for (i in 1:10) {
impi[[i]] <- glm(died ~ agecat + female_1 + insurance + mech +
transfer + iss + mxaisbr1 + maxais + cm_chf_1 +
cm_mets_1 + cm_liver_1 + cm_htn_c_1 + cm_bldloss_1 +
state, data = subset(imp, .imp == i), family = binomial)
}
preimpi = lapply(impi, FUN = predict, type = "response")

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