On Jun 30, 2012, at 8:47 PM, David Winsemius wrote:
On Jun 30, 2012, at 6:04 PM, Lekgatlhamang, lexi Setlhare wrote:
Hi
I have a follow up question, relating to subsetting to list items.
After using the list and min(sapply()) method to adjust the length
of the variables, I specify a dynamic regression equation using the
variables in the list. My list looks like this:
Dcr<-
list
(Dcre1
=
DCred1
,Dcre2
=DCred2,Dcre3=DCred3,Dbobc1=DBoBC1,Dbobc2=DBoBC2,Dbobc3=DBoBC3,...)
This should ahve been done like this:
Dcr<- data.frame(Dcre1=DCred1, Dcre2=DCred2, Dcre3=DCred3,
Dbobc1=DBoBC1, Dbobc2=DBoBC2, Dbobc3=DBoBC3)
By specifying the list items with names, I thought I could end by
referencing them (or subsetting the list) as, eg., Dcr$Dcre1 and
get DCred1, Dcr$Dbobc1 and get DBoBC1, etc so that the explanatory
variables of the equation can be easily associated with their
respective original names. This way, I would avoid specifying the
list as Dcr<-list(Dcr1, Dcr2, Dcr, 3..., Dcr15) and then subsetting
the list using Dcr[[1]][1:29], Dcr[[[2]][1:29], ..., Dcr[[15]]
[1:29] because the list has many variables (15) and referencing the
variables with numbers makes them lose their original names.
When I specify the list as Dcr<- list(Dcr1, Dcr2, ..., Dcr15), then
the regression equation specified as:
# Regression
regCred<- lm(Dcr[[1]][1:29]~Dcr[[2]][1:29]+Dcr[[3]][1:29]+Dcr[[4]]
[1:29]+Dcr[[5]][1:29]+Dcr[[6]][1:29]+...)
And the you could have done
regCred<- lm(Dcre1 ~ . , data=Dcr [ , 1:29] )
Oh, Nuts! I meant to type:
regCred<- lm(Dcre1 ~ . , data=Dcr [ 1:29, ] )
(Leaving out the , ...)
runs without problems - the results are shown here below:
Call:
lm(formula = Dcr[[1]][1:29] ~ Dcr[[2]][1:29] + Dcr[[3]][1:29] +
Dcr[[4]][1:29] + Dcr[[5]][1:29] + Dcr[[6]][1:29])
Residuals:
Min 1Q Median 3Q Max
-86.293 -33.586 -9.969 40.147 117.965
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 81.02064 13.28632 6.098 3.21e-06 ***
Dcr[[2]][1:29] -0.97407 0.11081 -8.791 8.20e-09 ***
Dcr[[3]][1:29] -0.27950 0.05899 -4.738 8.95e-05 ***
Dcr[[4]][1:29] -0.07961 0.04856 -1.639 0.115
Dcr[[5]][1:29] -0.07180 0.05515 -1.302 0.206
Dcr[[6]][1:29] -0.01562 0.02086 -0.749 0.462
But when I specify the list with names as shown above, then the
equation does not run - as shown by the following error message
# Regression
regCred<- lm(Dcr[[1]][1:29]~Dcr[[2]][1:29]+Dcr[[3]][1:29]+Dcr[[4]]
[1:29]+
+ Dcr[[5]][1:29]+Dcr$Dbobc3)
Error in model.frame.default(formula = Dcr[[1]][1:29] ~ Dcr[[2]]
[1:29] + :
variable lengths differ (found for 'Dcr$Dbobc3')
Dcr[[5]][1:29]+Dcr$Dbobc3[1:29])
Error: unexpected ')' in "Dcr[[5]][1:29]+Dcr$Dbobc3[1:29])"
NB: In the equation with error message, only the last term is
specified by referencing its name (ie., Dcr$Dbobc3[1:29]. Also note
that the error occurs whether I append '[1:29]' to Dcr$Dbobc or not.
How do I resolve this?
This still applies:
You should have offered str(Dcr)
David Winsemius, MD
West Hartford, CT
______________________________________________
R-help@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.