我在R中使用mlogit运行多项logit模型。我的示例数据框如下所示。
est best_adap ele area exp
1 A planting_date up 169 15
2 B crop_varieties up 441 18
3 C shade low 461 28
4 D crop_varieties mid 324 27
5 E crop_varieties low 312 20
6 F planting_date up 152 30
可通过以下代码访问此数据框。
structure(list(est = structure(1:6, .Label = c("A", "B", "C",
"D", "E", "F"), class = "factor"), best_adap = structure(c(2L,
1L, 3L, 1L, 1L, 2L), .Label = c("crop_varieties", "planting_date",
"shade"), class = "factor"), ele = structure(c(3L, 3L, 1L, 2L,
1L, 3L), .Label = c("low", "mid", "up"), class = "factor"), area = c(169L,
441L, 461L, 324L, 312L, 152L), exp = c(15L, 18L, 28L, 27L, 20L,
30L)), .Names = c("est", "best_adap", "ele", "area", "exp"), class = "data.frame", row.names = c(NA,
-6L))
简单模型的代码如下
#install.packages ("mlogit")
library(mlogit)
#Give factor Id for alternatives. "best_adap" has three alternatives
dat["best_adap_fac"]<- NA
dat$best_adap_fac <- factor(dat$best_adap, levels = c("planting_date" ,"crop_varieties","shade"),labels = c(1,2,3))
#Give factor Id for different levels of variable "ele"
dat["ele_fac"]<- NA
dat$ele_fac <- factor(dat$ele, levels = c("up" ,"low","mid"),labels = c(1,2,3))
#Convert experience (variable "exp") into three ranges
dat$exp_category<-cut(dat$exp, seq(0,30,10), labels = c(1:3))
#Covert the dataset from wide to long
dat_long<-mlogit.data(dat, varying = NULL, choice = "best_adap_fac", shape = "wide")
#Run Multinomial logit model. We don't have any variable that varies with the alternative but all vary only with individual.
mlogit_model1<-mlogit(best_adap_fac ~ 1| area, data=dat_long, reflevel = "3")
summary(mlogit_model1) #This runs!
mlogit_model2<-mlogit(best_adap_fac ~ 1| area + ele_fac, data=dat_long, reflevel = "3")
summary(mlogit_model2) #This works too
mlogit_model3<-mlogit(best_adap_fac ~ 1| area + ele_fac + exp_category, data=dat_long, reflevel = "3")
模型3不起作用并返回以下错误消息
Error in solve.default(H, g[!fixed]) :
Lapack routine dgesv: system is exactly singular: U[12,12] = 0
请有人帮我找到解决方案。
谢谢