[R] Problem with sample session

Jeff Newmiller jdnewmil at dcn.davis.CA.us
Fri May 30 23:02:28 CEST 2014


1. If you create a variable x or w or dummy, it is stored in the current environment. You can refer to it by the name x or w or dummy. If you create a column x in the data frame dummy, you can refer to it as dummy$x or dummy[["x"]]. That is a different x than the variable x in your current environment. If you execute attach(dummy), then your current environment becomes the dummy data frame and you can refer to the column x in the data frame as x rather than the variable discussed above. If you have attached dummy and you refer to a variable like w that doesn't exist in dummy, R will search the chain of environments. The first environment it finds after looking through dummy and failing is the environment that was previously your current environment, which does have a w variable, and it will use it.
2. The R lm function is not a swiss army knife... there are other functions to obtain those additional columns... in this case the predict and residuals functions would get that data. For example,
dummy[ , "y.fm" ] <- predict( fm )
dummy[ , "resid.fm" ] <- residuals( fm )
Read ?lm and pay attention to the see also and examples sections. 
3. Until you use the detach function, when you refer to x you will see the x that is a column in the dummy data frame. Afterward, you will have to use dummy$x to see that same value.
4. The chain of variable environments exists only within the RAM used by R, and has nothing too do with the disk directory structure. That was a concept from S (so I was told), not from R.
5. You are overdue to read the Posting Guide mentioned in the footer. In there, among other things, is advice to post in plain text. HTML tends to be corrupted when the mailing list strips the HTML, so we may not see what you think we see.
---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live Go...
                                      Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
--------------------------------------------------------------------------- 
Sent from my phone. Please excuse my brevity.

On May 30, 2014 1:18:04 PM PDT, Stephen Meskin <actuary at umbc.edu> wrote:
>Greg, Ista, (or anyone else),
>Let me take one last run at this problem.
>
>Consider the following extract from the Appendix A text:
>
>> > x <- 1:20
>> > w <- 1+sqrt(x)/2
>> > dummy <- data.frame(x=x, y=x+rnorm(x)*w)
>> > fm <- lm(y~x, data=dummy)
>> > fm1 <- lm(y~x, data=dummy, weight=1/w^2)
>>
>> >attach(dummy)
>> /Make the columns in the data frame visible as variables. /
>>
>> The following object is masked_by_.GlobalEnv:
>>
>> x
>
>In the above I have included only one comment, "Make the columns ... 
>visible as variables." from the text and
>only one response, "The following object is masked by ... : x."
>
>Stuff I don't understand:
>
> 1. The purpose of "attach" seems to be to make x and y visible but I
>    can already see them by entering the command "dummy" even after the
>    warning. So what does "attach do?
>2. What I would like to see is a table with 1st column x; 2nd column y;
>    3rd and 4th columns predicted ys from fm and fm1; plus possibly
>    columns of residuals and other stuff. Such tables don't seem to be
>    available according to the discussion in ?lm.
> 3. The warning about "attach" seems to say that there is an "x" in the
>    Global Environment that will mask the "x" that I am using. But that
>    is not happening in what I see. If I enter "> x" after the warning,
>    I still get 1,2,3,.... as before. What is the problem?
> 4. If I place the above R-script in a folder other than the R-console
>    that comes up when I first open R will that obviate the attach
>    "problem."
>
>
>/Stephen A Meskin/, PhD, FSA, MAAA
>Adjunct Assistant Professor of Mathematics, UMBC
>
>*Most people give you an anticipatory grin when you mention a
>/statistic/,
>frown doubtingly when you mention the plural /statistics/, and
>grunt and groan in a gurgle when you mention /a statistics course/.*//
>On 5/30/2014 12:20 PM, Greg Snow wrote:
>> If you pay attention and are careful not to use any variables names
>> that conflict then you do not need a work around (and the conflicts
>> function can help you see if there are any conflicts that you may
>need
>> to worry about).
>>
>> Probably the best work around is to use the with or within function
>> instead of attaching.  For a couple of quick commands these work
>great
>> and I prefer them to using attach.  But, sometimes for a long
>sequence
>> of commands attach is much more convenient and is fine to use as long
>> as you recognize the potential dangers and are careful.
>>
>> On Thu, May 29, 2014 at 3:56 PM, Stephen Meskin <actuary at umbc.edu>
>wrote:
>>> Thanks Greg for your response. Is there a work around?
>>>
>>> Of course this begs the question as to Why is attach part of the
>sample session in App. A of the introductory manual? All the commands
>are directly from App. A. Is it possible the configuration of R on my
>computer is not in accord with acceptable practice? I.e. Could my
>configuration be set so that attach works as App. A intends?
>>> If not then App. A needs to be changed to replace attach.
>>> If so, then App. A needs to provide instructing on appropriate
>configuration of R for newbies.
>>>
>>> Stephen Meskin
>>> Sent from my iPad
>>>
>>>> On May 29, 2014, at 1:06 PM, Greg Snow <538280 at gmail.com> wrote:
>>>>
>>>> This is a warning and in your case would not be a problem, but it
>is
>>>> good to think about and the reason why it is suggested that you
>avoid
>>>> using attach and be very careful when you do use attach.  What is
>>>> happening is that you first created a vector named 'x' in your
>global
>>>> workspace, you then create a data frame that contains a column that
>is
>>>> a copy of 'x' that is also named 'x' and the data frame also has
>>>> another column named 'y'.  You then later attach the data frame to
>the
>>>> search list (if you run the 'search()' command you will see your
>>>> search list).  This is convenient in that you can now access 'y' by
>>>> typing its name instead of something like 'dummy$y', but what
>happens
>>>> if you just type x?  The issue is that there are 2 objects on your
>>>> search path with that same name.  For your example it will not
>matter
>>>> much because they have the same value, but what if you run a
>command
>>>> like 'x <- 3', now you will see a single value instead of a vector
>of
>>>> length 20 which can lead to hard to find errors.  This is why R
>tries
>>>> to be helpful by warning you that there are multiple objects named
>'x'
>>>> and therefore you may not be accessing the one that you think.  If
>you
>>>> use attach without being careful it is possible to plot (or regress
>or
>>>> ...) one variable from one dataset against another variable from a
>>>> completely unrelated dataset and end up with meaningless results. 
>So,
>>>> if you use attach, be careful.  You may also want to look at the
>>>> followng functions for help with dealing with these issues:
>conflicts,
>>>> find, get, with, within
>>>>
>>>>> On Wed, May 28, 2014 at 11:55 PM, Stephen Meskin
><actuary at umbc.edu> wrote:
>>>>> While following the suggestion in the manual "An Introduction to
>R" to
>>>>> begin with Appendix A, I ran into the problem shown below about
>3/4 of
>>>>> the way down the 1st page of App. A.
>>>>>
>>>>> After using the function /attach/, I did not get visible columns
>in the
>>>>> data frame as indicated but the rather puzzling message emphasized
>below.
>>>>>
>>>>> I am running R version 3.1.0 (2014-04-10) using Windows XP. 
>Thanks in
>>>>> advance for your help.
>>>>>
>>>>>>> x<-1:20
>>>>>>> w<-1+sqrt(x)/2
>>>>>>> dummy<-data.frame(x=x, y=x+rnorm(x)*w)
>>>>>>> dummy
>>>>>>     x         y
>>>>>> 1   1  2.885347
>>>>>> ...
>>>>>>> fm<- lm(y ~ x, data=dummy)
>>>>>>> summary(fm)
>>>>>> Call:
>>>>>> ...
>>>>>>> fm1<- lm(y ~ x, data=dummy,weight=1/w^2)
>>>>>>> summary(fm1)
>>>>>> Call:
>>>>>> ...
>>>>>>> attach(dummy)
>>>>>> *_The following object is masked _by_ .GlobalEnv:
>>>>>>
>>>>>>     x_**_
>>>>>> _*
>>>>> --
>>>>> /Stephen A Meskin/, PhD, FSA, MAAA
>>>>> Adjunct Assistant Professor of Mathematics, UMBC
>>>>>
>>>>> **//
>>>>>
>>>>>         [[alternative HTML version deleted]]
>>>>>
>>>>> ______________________________________________
>>>>> 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.
>>>>
>>>>
>>>> --
>>>> Gregory (Greg) L. Snow Ph.D.
>>>> 538280 at gmail.com
>>>>
>>
>>
>
>
>	[[alternative HTML version deleted]]
>
>______________________________________________
>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