[R] Problem with plotting diagnostics - Error in object$coefficients : $ operator is invalid for atomic vectors

Manderscheid Katharina Katharina.Manderscheid at unilu.ch
Fri Nov 26 14:23:33 CET 2010


thanks for your suggestions.
i think i am getting some sort of idea where the problem lies.
i am working with a dataset which i attached. the objects of my regression model are derived from this dataset but, as far as i understand, only exist in the r environment.
i have detached the main dataset and created a new data frame containing just the four objects, and then i attached this new data frame, ran the model, did the plotting and: fine.
still, what i don't understand why it worked without this extra loop in R 2.11.1 and not anymore in R 2.12.0.

in order to reproduce the problem: i am working with the ESS data (http://www.europeansocialsurvey.org/) round 4, DE, and produced the four objects as follows:
satwirt <- as.numeric(stfeco)
satwirt[satwirt>11]<-NA
freunde <- as.numeric(sclmeet)
freunde[freunde>8]<-NA
gesund <- factor(c("gesund", "nicht gesund"))
gesund[as.numeric(health)<3]<-"gesund"
satlife <- as.numeric(stflife)
satlife[satlife>11] <-NA 
income <- factor(c("0-1100", "1111-1460", "1461-1840", "1841-2250", "2251-2710", "2711-3190", "3191-3770", "3771-4550", "4551-5870", "5870 und mehr"))
income[hinctnta=="J"] <- "0-1100"
income[hinctnta=="R"] <- "1111-1460"
income[hinctnta=="C"] <- "1461-1840"
income[hinctnta=="M"] <- "1841-2250"
income[hinctnta=="F"] <- "2251-2710"
income[hinctnta=="S"] <- "2711-3190"
income[hinctnta=="K"] <- "3191-3770"
income[hinctnta=="P"] <- "3771-4550"
income[hinctnta=="D"] <- "4551-5870"
income[hinctnta=="H"] <- "5870 und mehr"

the problem appears when calling
par(mfrow=c(2,2))
plot(reg.satlife)
i get the first two, but not the second two graphs. which kind of does not make sense to me yet.

thanks again for your support,
katharina


-----Ursprüngliche Nachricht-----
Von: Peter Ehlers [mailto:ehlers at ucalgary.ca] 
Gesendet: Freitag, 26. November 2010 12:49
An: Manderscheid Katharina
Cc: 'r-help at r-project.org'
Betreff: Re: AW: [R] Problem with plotting diagnostics - Error in object$coefficients : $ operator is invalid for atomic vectors

On 2010-11-25 23:59, Manderscheid Katharina wrote:
> hi peter,
>
> thanks for your reply. i was kind of hoping/thinking that others have encountered the same problem too.
>
> i try to provide a reproducable example.
>
> my variables are the following (just the cases 1:50). the whole dataset has about 2800 cases.
>> satlife[1:50]
>   [1]  9 10 11  8 11 10 11  6  7  8  3  8 10  9  8  9  7  7  9  8  8  
> 7 10  8  9 [26]  5  7  9  9 11 11  5  3  9  8  9  3  9  6  8  5  6  6  
> 6  9 11  6  5  9  9
>> income[1:50]
>   [1] 1841-2250 1111-1460 1841-2250 1841-2250 2251-2710 2711-3190 1461-1840
>   [8] 3771-4550 4551-5870 3191-3770 1841-2250 1461-1840 3771-4550 2251-2710
> [15] 2251-2710 2711-3190 0-1100    1841-2250<NA>       1841-2250 1111-1460
> [22]<NA>       3771-4550<NA>       1461-1840<NA>       2711-3190 1461-1840
> [29] 0-1100    1461-1840<NA>       1841-2250<NA>       3191-3770<NA>
> [36]<NA>       <NA>       2251-2710 2711-3190<NA>       <NA>       <NA>
> [43] 2711-3190 2251-2710 3771-4550 3771-4550<NA>       <NA>       4551-5870
> [50]<NA>
> 10 Levels: 0-1100 1111-1460 1461-1840 1841-2250 2251-2710 ... 5870 und 
> mehr
>> satwirt[1:50]
>   [1]  3  5  6  1  5  5  4  1  4  6  4  1  4  8  1  4  6  5 10  7  8  
> 1  2  4  4 [26]  5  4  4  8 NA  3  4  5  8  6 NA  6  8  6  4  4  3  6  
> 8  6  3  4  1  9  8
>> freunde[1:50]
>   [1] 4 5 7 4 6 7 7 3 3 4 2 4 4 5 3 6 7 4 5 2 4 4 5 4 4 6 5 4 4 6 6 6 
> 4 4 5 4 5 6 [39] 3 7 4 5 6 5 5 5 4 2 3 5
>> gesund[1:50]
>   [1] gesund       gesund       gesund       gesund       nicht gesund
>   [6] gesund       gesund       nicht gesund nicht gesund gesund
> [11] gesund       nicht gesund gesund       gesund       nicht gesund
> [16] gesund       gesund       nicht gesund gesund       gesund
> [21] gesund       gesund       gesund       gesund       nicht gesund
> [26] gesund       gesund       gesund       nicht gesund gesund
> [31] gesund       gesund       nicht gesund gesund       gesund
> [36] nicht gesund nicht gesund nicht gesund nicht gesund gesund
> [41] nicht gesund gesund       nicht gesund gesund       nicht gesund
> [46] gesund       gesund       gesund       gesund       gesund
> Levels: gesund nicht gesund
>
> and this is the regression model:
>> reg.satlife<- lm(satlife ~ as.numeric(income)+ freunde + gesund+ 
>> satwirt)
>
> the problem appears when calling
>> par(mfrow=c(2,2))
>> plot(reg.satlife)
>
> the problem persists also when running the model without the factors income and gesund.
>
> do you need more information?

Dear Katharina,

Yes, we do need more information.
First, what you provide above is not easily *reproducible*.
Try to provide data with dput() or post it on some accessible website.

But before you do that, run your regression on the first 50 cases of your data (what you provide above, I assume). It works for me (although I can't see why you would want the "as.numeric(income)" term).

My guess is that there's something wrong with your data.
I would try:
1. put your variables into a data.frame; 2. str(...your data.frame...); 3. run regressions with single predictors; 4. run regressions with two predictors; etc, until the "problem" appears.

It's almost certain that the problem is not with R.

Peter Ehlers


>
> thanks,
> katharina
>
> ________________________________________
> Von: Peter Ehlers [ehlers at ucalgary.ca]
> Gesendet: Donnerstag, 25. November 2010 21:54
> An: Manderscheid Katharina
> Cc: 'r-help at r-project.org'; 'Duncan Murdoch'
> Betreff: Re: [R] Problem with plotting diagnostics - Error in 
> object$coefficients : $ operator is invalid for atomic vectors
>
> On 2010-11-25 02:25, Manderscheid Katharina wrote:
>> this problem seems to only exist in R 2.12.0 but not in R  2.11.1.
>> any ideas? a bug?
>>
>
> Duncan *did* say that he was using  R 2.12.0. So that's not likely to 
> be the problem. Most of the time, when users claim that a problem 
> exists in a new version that did not exist in an older version, it's 
> due to a change in the user's setup or to not updating packages or to 
> not checking the NEWS file.
>
> Since you still have not provided a *reproducible* example, it's not 
> likely that anyone can help. Can't you make up a small example that 
> shows exactly how you are using lm() and that will generate the error?
>
> Peter Ehlers
>
>>
>> ----------------------------------------------
>> dr. katharina manderscheid
>>
>> soziologisches seminar
>> universität luzern
>>
>> kasernenplatz 3
>> 6000 luzern 7
>>
>> tel. ++41 41 228 4657
>>
>> web: http://www.unilu.ch/deu/dr.-katharina-manderscheid_346380.aspx
>>
>> -----Ursprüngliche Nachricht-----
>> Von: Duncan Murdoch [mailto:murdoch.duncan at gmail.com]
>> Gesendet: Mittwoch, 17. November 2010 16:33
>> An: Manderscheid Katharina
>> Cc: 'r-help at r-project.org'
>> Betreff: Re: [R] Problem with plotting diagnostics - Error in 
>> object$coefficients : $ operator is invalid for atomic vectors
>>
>> On 17/11/2010 10:28 AM, Manderscheid Katharina wrote:
>>> hi all,
>>> after fitting a multiple linear regression
>>> model<- lm(y ~ a + b+ c+d)
>>> i wanted to plot diagnostics
>>> plot(model)
>>> but get the error message
>>> Error in object$coefficients : $ operator is invalid for atomic vectors.
>>> which does not make a lot of sense, since there is no "$" - i am working with an attached dataset.
>>> can anyone help, please??
>>> thanks a lot,
>>> kat
>>
>>
>> I just tried those lines (with fake data for a,b,c,d and y) and got no error message.  I was using R 2.12.0.
>>
>> I think you need to show us a reproducible example, and the
>> sessionInfo() to go with it, to help with this.
>>
>> Duncan Murdoch
>>
>> ______________________________________________
>> 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.
>



More information about the R-help mailing list