English 中文(简体)
在R中采用log酸it的浓度
原标题:Error with levels using mlogit in R
  • 时间:2011-11-10 20:27:59
  •  标签:
  • r
  • levels

我对水平有些麻烦。 介绍如下:

library(mlogit)

panel.datasm = data.frame(
    cbind( 
        round(runif(100, min=1, max=6)), 
        rep(1:20,each=5), runif(100, min=0, max=1), 
        runif(100, min=0, max=6), 
        runif(100, min=2, max=6) , 
        runif(100, min=0, max=1), 
        runif(100, min=0, max=6), 
        runif(100, min=2, max=6)  ))
names(panel.datasm) = c("choice", "id", "data_1991","data_1992",
  "data_1993", "data2_1991", "data2_1992","data2_1993") 


logit.data <- mlogit.data(panel.datasm, id = "id", choice = "choice", 
    varying= 3:5, shape = "wide", sep = "_")

保留错误:<代码>英寸(数据[]],备选案文): 不同层次的因素有

我也尝试人工分配等级:

panel.datasm$id= factor(
    panel.datasm$id, 
    levels = sort(as.character(unique(panel.datasm$id)))  )

我已经尝试过一些事情,可以列举出什么是错的。 比较:

data("Electricity", package = "mlogit")
head(Electricity)
Electr <- mlogit.data(Electricity, id = "id", choice = "choice", 
    varying = 3:26, shape = "wide", sep = "")

就我所知,与我的数据格式相同。 这里的情况如何? 我的脑.着。

最佳回答

我认为,我已追踪这一问题。 www.un.org/Depts/DGACM/index_french.htm

您更改<代码>数据>第一栏目,在<代码>1991:1993年之间有价值 它将开展工作。

panel.datasm = data.frame(
    cbind( 
        sample(1991:1993, 100, replace=TRUE), 
        rep(1:20,each=5), runif(100, min=0, max=1), 
        runif(100, min=0, max=6), 
        runif(100, min=2, max=6) , 
        runif(100, min=0, max=1), 
        runif(100, min=0, max=6), 
        runif(100, min=2, max=6)  ))
names(panel.datasm) = c("choice", "id", "data_1991","data_1992",
    "data_1993", "data2_1991", "data2_1992","data2_1993") 


logit.data <- mlogit.data(panel.datasm, id = "id", choice = "choice", 
    varying= 3:5, shape = "wide", sep = "_") 

结果:

head(logit.data)
       choice id  alt       data     data2 chid
1.1991  FALSE  1 1991 0.03540498 0.9726110    1
1.1992  FALSE  1 1992 5.85285278 2.7973798    1
1.1993   TRUE  1 1993 5.80795641 3.7360297    1
2.1991   TRUE  1 1991 0.59255235 0.2564928    2
2.1992  FALSE  1 1992 5.81443351 3.0820215    2
2.1993  FALSE  1 1993 2.11699854 5.4161634    2

如果你现在将其与<条码>电子数据相比较,差异是显而易见的。 选择为<代码>1:4的通知,每一备选案文从1到4不等。

head(Electricity)
  choice id pf1 pf2 pf3 pf4 cl1 cl2 cl3 cl4 loc1 loc2 loc3 loc4 wk1 wk2 wk3 wk4
1      4  1   7   9   0   0   5   1   0   5    0    1    0    0   1   0   0   1
2      3  1   7   9   0   0   0   5   1   5    0    0    1    0   1   1   0   0
3      4  1   9   7   0   0   5   1   0   0    0    0    0    1   0   1   1   0
4      4  1   0   9   7   0   1   1   0   5    0    0    1    0   1   0   0   1
5      1  1   0   9   0   7   0   1   0   5    1    0    0    0   0   1   0   1
6      4  1   0   9   0   7   0   0   1   5    0    0    1    0   0   0   0   1
问题回答

问题是,由<代码>reshape创建的<编码>并非独一无二,造成麻烦。 这里是一个快速解决方案。 你们需要添加一个<条码>。 我使用了<代码>index的功能>。 你们也可以使用其他方法,我也可以说。

mlogit.data(panel.datasm, choice =  choice , id =  id , shape =  wide , 
 varying = 3:8, sep = "_", chid.var = 1:NROW(index))

        choice id  alt     data      data2
1.1991  FALSE  1 1991 0.4769187 0.97381645
1.1992  FALSE  1 1992 3.2998748 0.70989021
1.1993  FALSE  1 1993 5.6199917 5.53069555
2.1991  FALSE  1 1991 0.3615670 0.02066214
2.1992  FALSE  1 1992 2.0461820 0.41804600
2.1993  FALSE  1 1993 2.2764992 3.93337758

The error comes from the reshape package. It is unable to determine the time element when converting the data.

The mlogit help guide ?mlogit.data provides the solution to this under the option "alt.levels" stating: "the name of the alternatives: if null, for a wide data.frame, they are guessed from the variable names and the choice variable (both should be the same)".

由于你没有给出替代物的名称,所以你正在猜测,不能确定这些替代物。 然后是人工提供这些名字。 提供问题中提供的数据如下:

logit.data <- mlogit.data(panel.datasm, id = "id", choice = "choice", 
                      varying= 3:8, shape = "wide", sep = "_",
                      alt.levels = c("data_1991","data_1992","data_1993", "data2_1991", "data2_1992", "data2_1993"))

页: 1





相关问题
How to plot fitted model over observed time series

This is a really really simple question to which I seem to be entirely unable to get a solution. I would like to do a scatter plot of an observed time series in R, and over this I want to plot the ...

REvolution for R

since the latest Ubuntu release (karmic koala), I noticed that the internal R package advertises on start-up the REvolution package. It seems to be a library collection for high-performance matrix ...

R - capturing elements of R output into text files

I am trying to run an analysis by invoking R through the command line as follows: R --no-save < SampleProgram.R > SampleProgram.opt For example, consider the simple R program below: mydata =...

R statistical package: wrapping GOFrame objects

I m trying to generate GOFrame objects to generate a gene ontology mapping in R for unsupported organisms (see http://www.bioconductor.org/packages/release/bioc/vignettes/GOstats/inst/doc/...

Changing the order of dodged bars in ggplot2 barplot

I have a dataframe df.all and I m plotting it in a bar plot with ggplot2 using the code below. I d like to make it so that the order of the dodged bars is flipped. That is, so that the bars labeled "...

Strange error when using sparse matrices and glmnet

I m getting a weird error when training a glmnet regression. invalid class "dgCMatrix" object: length(Dimnames[[2]]) must match Dim[2] It only happens occasionally, and perhaps only under larger ...

Generating non-duplicate combination pairs in R

Sorry for the non-descriptive title but I don t know whether there s a word for what I m trying to achieve. Let s assume that I have a list of names of different classes like c( 1 , 2 , 3 , 4 ) ...

Per panel smoothing in ggplot2

I m plotting a group of curves, using facet in ggplot2. I d like to have a smoother applied to plots where there are enough points to smooth, but not on plots with very few points. In particular I d ...