[R] Two Noobie questions
Prof Brian Ripley
ripley at stats.ox.ac.uk
Thu Jan 8 09:14:22 CET 2009
On Thu, 8 Jan 2009, Neil Beddoe wrote:
> You can also use subscripts to get at things with a bit of playing around.
You can, but it is easier not to fight R to do so. Much more transsparent
is:
cf <- coef(summary(<lm fit>)
cf[2,2] # index as a matrix.
You are
a) indexing a matrix as a vector
b) using [[]] on a numeric vector, which is unneded
c) indexing a list by number where indexing by name is simpler and using
the extractor function coef() is even simpler.
>
>> summary(lm(x~seq(1,length(x),1)))
>
> Call:
> lm(formula = x ~ seq(1, length(x), 1))
>
> Residuals:
> Min 1Q Median 3Q Max
> -40.0961 -15.5289 -0.6489 12.7488 41.0107
>
> Coefficients:
> Estimate Std. Error t value Pr(>|t|)
> (Intercept) 165.259602 1.620906 101.955 <2e-16 ***
> seq(1, length(x), 1) -0.048711 0.005551 -8.775 <2e-16 ***
> ---
> Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
>
> Residual standard error: 18.19 on 503 degrees of freedom
> Multiple R-squared: 0.1328, Adjusted R-squared: 0.131
> F-statistic: 77 on 1 and 503 DF, p-value: < 2.2e-16
>
>> summary(lm(x~seq(1,length(x),1)))[[4]][[4]]
> [1] 0.005551145
>> summary(lm(x~seq(1,length(x),1)))[[4]][[2]]
> [1] -0.04871091
>>
>
> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of AllenL
> Sent: 06 January 2009 19:48
> To: r-help at r-project.org
> Subject: Re: [R] Two Noobie questions
>
>
> Thanks for your help!
>
> I combined the above two to get the following, which seems to work (if somewhat inelegant):
>
> int.List<-unlist(lapply(lmList, function(x) {coef(x)[1]}),use.names=FALSE) lmList is my list of lm objects.
> -Allen
>
>
>
>
>
> David Winsemius wrote:
>>
>>
>> On Jan 6, 2009, at 1:50 PM, AllenL wrote:
>>
>>>
>>> 1. I have a list of lm (linear model) objects. Is it possible to
>>> select, through subscripts, a particular element (say, the intercept)
>>> from all the models? I've tried something like this:
>>
>> ?coef
>> if your list of models is ml, then perhaps something like this
>> partially tested idea:
>>
>> lapply(ml, function(x) coef(x)[1] )
>>
>> This is what I get using that formulation an available logistic model:
>>
>> > coef(lr.TC_HDL_BMI)[1]
>> Intercept
>> -6.132448
>>
>>
>>>
>>>
>>> List[[1:length(list)]][1]
>>> All members of the list are similar. My goal is to have a list of the
>>> intercepts and lists of other estimated parameters. Is it better to
>>> convert
>>> to a matrix? How to do this?
>>>
>>> 2. Connected to this, how do I convert from a list back to a vector?
>>> This
>>> problem arose from using "split" to split a vector by a factor, then
>>> selecting a subset of this (ie. length>10), leaving me with subset
>>> list of
>>> my original. Unsplit(newList, factor) doesn't work, presumably due
>>> to my
>>> removal of some values. Thoughts?
>>
>> ?unlist
>>
>> > ll <- list(1,2,3,4)
>> > ll
>> [[1]]
>> [1] 1
>>
>> [[2]]
>> [1] 2
>>
>> [[3]]
>> [1] 3
>>
>> [[4]]
>> [1] 4
>>
>> > unlist(ll)
>> [1] 1 2 3 4
>> > str(unlist(ll))
>> num [1:4] 1 2 3 4
>> > is.vector(unlist(ll))
>> [1] TRUE
>>
>> --
>> David Winsemius
>>>
>>>
>>> Thanks!
>>> -Allen
>>>
>>>
>>>
>>> --
>>> View this message in context:
>>> http://www.nabble.com/Two-Noobie-questions-tp21316554p21316554.html
>>> Sent from the R help mailing list archive at Nabble.com.
>>>
>>> ______________________________________________
>>> R-help at r-project.org mailing list
>>> 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.
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> 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.
>>
>>
>
> --
> View this message in context: http://www.nabble.com/Two-Noobie-questions-tp21316554p21317630.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> 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.
>
> .
>
> This message is intended only for the use of the person(s) to whom it is addressed. It may contain information which is privileged and confidential. Accordingly any unauthorised use is strictly prohibited. If you are not the intended recipient, please contact the sender as soon as possible.
>
> It is not intended as an offer or solicitation for the purchase or sale of any financial instrument or as an official confirmation of any transaction, unless specifically agreed otherwise. All market prices, data and other information are not warranted as to completeness or accuracy and are subject to change without notice. Any opinions or advice contained in this Internet email are subject to the terms and conditions expressed in any applicable governing Marble Bar Asset Management LLP's terms and conditions of business or client agreement letter. Any comments or statements made herein do not necessarily reflect those of Marble Bar Asset Management LLP.
>
> Marble Bar Asset Management LLP is regulated and authorised by the FSA.
>
> ______________________________________________
> R-help at r-project.org mailing list
> 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.
>
--
Brian D. Ripley, ripley at stats.ox.ac.uk
Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel: +44 1865 272861 (self)
1 South Parks Road, +44 1865 272866 (PA)
Oxford OX1 3TG, UK Fax: +44 1865 272595
More information about the R-help
mailing list