[R] Referencing a component of a large object (Error: slot NULL)

William Dunlap wdunlap at tibco.com
Sat Aug 29 17:59:18 CEST 2015


Recall that List$name is usually equivalent to List[["name"]] (2 square
brackets),
not List["name"].

Look at names(mi.control.i at data) to see what your paste command
ought to be producing.

Unless the names are important you can use ...data[[j]] to index the
components
in data by number instead of by name (assuming data is a list).


Bill Dunlap
TIBCO Software
wdunlap tibco.com

On Sat, Aug 29, 2015 at 6:33 AM, Andrea Lamont <alamont082 at gmail.com> wrote:

> Sorry, keyboard shortcuts sent too soon.
>
> That still doesn't work. Actually, the way I "stumbled" on those single
> quotes was that it didn't reference the object without it. Ihave created a
> smaller version of the code/computations for reproducibility. I thought
> this may help troubleshoot? It requires package mi, but since i reduced the
> size, it runs in about a minute.
>
> dataGen=function(N,p,signp,seed){
>  set.seed(2398)
> Xsign=rbinom(N*signp,1,.05)
> df1=data.frame(matrix(Xsign,nrow=N, ncol=signp))
>
> Xns=rbinom(N*(p-signp),1,.5)
>  df2=data.frame(matrix(Xns,nrow=N,ncol=(p-signp)))
>
> df=cbind(df1,df2)
> names(df) <- paste("X", 1:p, sep="")
>  df$obs.txt=rep(0:1,N/2)
> df$TE=(-5+1.1*df$X1+
>  1.4*df$X2+
>  2.5*df$X3+
>   1.3*df$X4+
>          2.2*df$X5+
>  1.6*df$X6+
>  1.8*df$X7)
> #df$oddsTE=exp(df$TE)
> df$pTE= (1/(1+exp(-1*(df$TE))))
> df$pTEneg=df$pTE*-1
> #df$pTEoriginal=df$oddsTE/(1+df$oddsTE)
>  seed=set.seed(seed)
>  df$y.obs.control=rbinom(N,1,.02)#observed y value under control
>  df$y.obs.tx= ifelse(df$obs.txt==1, rbinom(N,1, df$pTE),NA) #observed y
> value under TX
>  df$Y=ifelse(df$obs.txt==0,df$y.obs.control,df$y.obs.tx) #observed Y value
>  df$y.obs.control=ifelse(df$obs.txt==0,df$y.obs.control,NA) #observed y
> value under control
>  df$ob=rep(0:1,each=N/2)
>  df$sim=rep(length(seed),each=N)
>  return(df)}
>
>  library(mi)
> x=dataGen(10000,7,7,542)
> txt.imp=as.data.frame(x[,c(13,1:7)])
> cont.imp=as.data.frame(x[,c(12,1:7)])
> mdf <- missing_data.frame(txt.imp)
>
> mi.control.i<-mi (mdf,n.chains=2)
> str(mi.control.i)
>
> #Get coefficients for each chain (imputation)
> tf <- vector("list", 7)
>
> for (j in 1:7){
>         s=noquote(paste0("chain:",j))
> s
>       tf[[j]] =  mi.control.i at data[s]@variables$y.obs.tx at parameters[30,]
> }
>
>
> On Sat, Aug 29, 2015 at 9:31 AM, Andrea Lamont <alamont082 at gmail.com>
> wrote:
>
>> That still doesn't work. Actually, the way I "stumbled" on those single
>> quotes was that it didn't reference the object without it. Ihave created a
>> smaller version of the code/computations for reproducibility. I thought
>> this may help troubleshoot? It requires package mi, but since i reduced the
>> size, it runs in about a minute.
>>
>>
>>
>> On Fri, Aug 28, 2015 at 11:59 PM, William Dunlap <wdunlap at tibco.com>
>> wrote:
>>
>>>    s <- paste("'chain:",j,"'" ,sep="")
>>>
>>> You don't want the single quotes around the string you are
>>> constructing.  Try
>>>    s <- paste0("chain:", j)
>>> which will give you, e.g.,
>>>    "chain:1"
>>> instead of
>>>    "'chain:1'"
>>>
>>> Bill Dunlap
>>> TIBCO Software
>>> wdunlap tibco.com
>>>
>>> On Fri, Aug 28, 2015 at 6:08 PM, Andrea Lamont <alamont082 at gmail.com>
>>> wrote:
>>>
>>>> Hm.
>>>>
>>>> It still doesn't seem to be working and has me completely stumped.
>>>>
>>>> This works:
>>>> mi.control.i at data[['chain:1']]@variables$y.obs.tx at parameters[30,]
>>>>
>>>> But this doesn't:
>>>> > mi.control.i at data[[s]]@variables$y.obs.tx at parameters[30,]
>>>> Error: trying to get slot "variables" from an object of a basic class
>>>> ("NULL") with no slots
>>>> > s
>>>> [1] "'chain:1'"
>>>>
>>>> On Fri, Aug 28, 2015 at 5:52 PM, William Dunlap <wdunlap at tibco.com>
>>>> wrote:
>>>>
>>>>>
>>>>>   tf <- vector("list", numberofchains)
>>>>>   for (j in 1:numberofchains){
>>>>>       s <- paste("'chain:",j,"'" ,sep="")
>>>>>       tf[[j]] =  mi.control.i at data$s at variables$y.obs.tx at parameters
>>>>> [30,]
>>>>>   }
>>>>>
>>>>>
>>>>> If you want the component whose name is the value of the variable 's'
>>>>> use data[[s]].
>>>>>
>>>>> The syntax 'data$s' means to get the component of 'data' called "s",
>>>>> usually the same as 'data[["s"]]'.
>>>>>
>>>>>
>>>>> Bill Dunlap
>>>>> TIBCO Software
>>>>> wdunlap tibco.com
>>>>>
>>>>> On Fri, Aug 28, 2015 at 12:08 PM, Andrea Lamont <alamont082 at gmail.com>
>>>>> wrote:
>>>>>
>>>>>> I am running a simulation and need to refer to a matrix of parameters
>>>>>> from
>>>>>> a large object. Here is a snippet of the object structure itself:
>>>>>>
>>>>>> Formal class 'mi' [package "mi"] with 3 slots
>>>>>>   ..@ call       : language .local(y = y, n.chains = ..2, max.minutes
>>>>>> = 20000)
>>>>>>   ..@ data       :List of 100
>>>>>>   .. ..$ chain:1  :'data.frame':    10000 obs. of  76 variables:
>>>>>> Formal class 'missing_data.frame' [package "mi"] with 17 slots
>>>>>>   .. .. .. ..@ .Data       : list()
>>>>>>   .. .. .. ..@ variables   :List of 76
>>>>>>   .. .. .. .. ..$ y.obs.tx:Formal class 'binary' [package "mi"] with
>>>>>> 27 slots...
>>>>>>
>>>>>> The parameter list I need can be referenced by:
>>>>>>
>>>>>> mi.control.i at data$'chain:1'@variables$y.obs.tx at parameters[30,]
>>>>>>
>>>>>> Since this is a simulation, I would like to grab the parameters from
>>>>>> each
>>>>>> chain and join them together in a matrix. I have this:
>>>>>>
>>>>>> tf <- vector("list", numberofchains)for (j in 1:numberofchains){
>>>>>>   s <- paste("'chain:",j,"'" ,sep="")
>>>>>>   tf[[j]] =  mi.control.i at data$s at variables$y.obs.tx at parameters[30,]}
>>>>>>
>>>>>> Within the loop, however, the reference to the object (tf[[j]]) does
>>>>>> not
>>>>>> work. I get an error that reads
>>>>>>
>>>>>> Error: trying to get slot "variables" from an object of a basic class
>>>>>> ("NULL") with no slots
>>>>>>
>>>>>>
>>>>>> I am happy to send reproducible code, however, I obtain this object
>>>>>> through
>>>>>> the package 'mi' and is computationally intensive. I'm not sure if it
>>>>>> makes
>>>>>> it easier for folks. Let me know.
>>>>>>
>>>>>> Any ideas?
>>>>>>
>>>>>> --
>>>>>> Andrea Lamont, PhD
>>>>>> Post-Doctoral Fellow
>>>>>> University of South Carolina
>>>>>> Columbia, SC 29208
>>>>>>
>>>>>> *Please consider the environment before printing this email.*
>>>>>>
>>>>>>         [[alternative HTML version deleted]]
>>>>>>
>>>>>> ______________________________________________
>>>>>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>>>>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>>>>> PLEASE do read the posting guide
>>>>>> http://www.R-project.org/posting-guide.html
>>>>>> and provide commented, minimal, self-contained, reproducible code.
>>>>>>
>>>>>
>>>>>
>>>>
>>>>
>>>> --
>>>> Andrea Lamont, PhD
>>>> Post-Doctoral Fellow
>>>> University of South Carolina
>>>> Columbia, SC 29208
>>>>
>>>> *Please consider the environment before printing this email.*
>>>>
>>>
>>>
>>
>>
>> --
>> Andrea Lamont, PhD
>> Post-Doctoral Fellow
>> University of South Carolina
>> Columbia, SC 29208
>>
>> *Please consider the environment before printing this email.*
>>
>
>
>
> --
> Andrea Lamont, PhD
> Post-Doctoral Fellow
> University of South Carolina
> Columbia, SC 29208
>
> *Please consider the environment before printing this email.*
>

	[[alternative HTML version deleted]]



More information about the R-help mailing list