I'm trying to run a weighted squares regression, after creating my weights and trying to add it to my regression function I receive the following error:
Error in model.frame.default(formula = CO2_pc_cmice1 ~ GDP_pc_cmice1_C + :
variable lengths differ (found for '(weights)')
The lm
model has 31 rows and the weights I've created are also 31, I've checked to see if there are NA
s in either of these and there are not. There are some negative numbers, although I'd be surprised if this was the issue. I've run the formula using both na.action = na.omit
and na.action = na.exclude
I'm also running this with a regression with a sample of 99 and I get the same issue.
My regression is
LinearCO2_lowerF <- (lm(CO2_pc_cmice1 ~ PolCiv_incPressFreedom_C + CorpInf_cmice1_C +
Gov_cmicepos1_C + LitGini_umice_C +
GDP_pc_cmice1_C + PopDensity_cmice1_C +
TradeOpen_cmice1_C + Urban_cmice1_C +
poly(Oil_coal_umice_C,2),
data = mydata_completemice2,
subset = IncomeL == "L"))
Weights created
wtsco2low <- 1/fitted( lm(abs(residuals(LinearCO2_lowerF))~fitted(LinearCO2_lowerF)) )^2
And the regression with weights
LinearCO2_lowerFw <- lm(CO2_pc_cmice1 ~ GDP_pc_cmice1_C + PolCiv_incPressFreedom_C +
CorpInf_cmice1_C + Gov_cmicepos1_C +
LitGini_umice_C + PopDensity_cmice1_C +
TradeOpen_cmice1_C + Urban_cmice1_C +
poly(Oil_coal_umice_C,2),
data = mydata_completemice2,
subset = IncomeL == "L",
weights = wtsco2low,
na.action = na.omit)
(Have also tried with na.exlude
)
Is anyone able to help?