Reputation: 49
I am using multinomial regression to get the probability of belonging to four sub-groups for 500,000 regions. The data.frame looks like this:
Regions groupadmit mid-pop
1 2 1764
2 3 1254
25 1 1452
674 4 2665
3001 2 1097
56 3 9864
98 1 2675
500,000 .... .....
I wrote the following code:
library (nnet)
mlogit<- multinom(groupadmit~mid_pop, data = admissionLSOA1)
probs <- predict(mlogit, type="probs")
The codes work fine till this point, giving the probability of belonging to each group (1, 2, 3, 4) for each observation (region).
Probabilities:
Regions groupadmit1 groupadmit2 groupadmit3 groupadmit4
52 0.2484091 0.2494408 0.2505393 0.2516109
97 0.2483949 0.2494358 0.2505441 0.2516252
1300 0.2483253 0.2494112 0.2505676 0.251695
287 0.2483623 0.2494242 0.2505551 0.2516584
500,000 .... ..... .... ....
But, when I go to weight the sample (regions) according to their probability, it brings back the following error:
Warning message:
In wts[groupadmit == 1] <- probs[groupadmit == 1, 1]/probs[groupadmit == :
number of items to replace is not a multiple of replacement length
What I am doing is weighting the regions according to their probability of belonging to each groupadmit proportional to the probability of belonging to groupadmit one in order to balance any chance for selection bias. It is very similar to inverse probability weighting. The codes are:
wts[groupadmit==1] <- probs[groupadmit==1,1]/probs[groupadmit==1,1]
wts[groupadmit==2] <- probs[groupadmit==2,1]/probs[groupadmit==2,2]
wts[groupadmit==3] <- probs[groupadmit==3,1]/probs[groupadmit==3,3]
wts[groupadmit==4] <- probs[groupadmit==4,1]/probs[groupadmit==4,4]
But, the above error comes up whenever I do the the analysis. May someone please help me to understand why I get this error and how can I solve it?
Many thanks in advance
Upvotes: 0
Views: 174
Reputation: 11649
Why R complains?
Warning message:
In wts[groupadmit == 1] <- probs[groupadmit == 1, 1]/probs[groupadmit == :
number of items to replace is not a multiple of replacement length
it means that, the right handside of you assign (<-)
is bigger than, what you have on the left handside which is wts[groupadmit==1]
Therefore, i suggest you to do:
length(probs[groupadmit==1,1]/probs[groupadmit==1,1])
and then
length(wts[groupadmit==1])
Then i suppose, it shows the lefthand side is smaller.
Then simply run
wts[groupadmit==1] <- probs[groupadmit==1,1]/probs[groupadmit==1,1]
and finnally print
wts[groupadmit==1]
Solution:
A quick fix is to use rbind
to build your wts
:
wts<-rbind(probs[groupadmit==1,1]/probs[groupadmit==1,1],
probs[groupadmit==2,1]/probs[groupadmit==2,2],
probs[groupadmit==3,1]/probs[groupadmit==3,3],
probs[groupadmit==4,1]/probs[groupadmit==4,4])
Upvotes: 0