[R] Problem with sample session

Ista Zahn istazahn at gmail.com
Fri May 30 22:48:36 CEST 2014


Hi Stephen,

See in line.

On Fri, May 30, 2014 at 4:18 PM, 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:
>
> 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?

It makes them "visible" in the sense that you can refer to them
without referring to dummy: try

rm(list=ls()) ## delete everything from your workspace

dummy <- data.frame(x=1:20) # data.frame containing x
dummy$w <- 1+sqrt(dummy$x)/2 # add "w" column to dummy
dummy$y <- dummy$x + dummy$x + rnorm(dummy$x) * dummy$w # add "y" column

# x is not available
x #Error: object 'x' not found
#...exept as an element of dummy
dummy$x

attach(dummy)
#   Make the columns in the data frame visible as variables.
x # x is now available as "x", as well as "dummy$x"


> 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.

In R it is common to calculate things only as you need them. The
predicted and residual values are not calculated by lm, but after the
fact by predict.lm() and residuals.lm(). For example:

fm <- lm(y~x, data=dummy)
fm1 <- lm(y~x, data=dummy, weight=1/w^2)

dummy$yhat.fm <- predict(fm)
dummy$yhat.fm1 <- predict(fm1)
dummy$yresid <- residuals(fm1)
dummy


> 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.

Yes it is.

 If I enter "> x" after the warning, I still get
> 1,2,3,.... as before. What is the problem?

That 1,2,3 ... you are seeing comes from the "x" that was defined earlier, by

x <- 1:20

_not_ from the x in dummy nor the x attached from dummy. Try this:

rm(list=ls())
x <- 1:5
w <- 1+sqrt(x)/2
dummy <- data.frame(x=x, y=x+rnorm(x)*w)
attach(dummy)

You now have three "x" variables: the one created with "x <- 1:5" (in
the global environment), the one in dummy, and the attached one copied
from dummy. This makes it easy to become confused about which "x" you
are getting. Consider:

x <- 1 ## changes x in the global workspace, but not dummy$x nor the
attached copy of dummy$x
dummy$x <- 2 # changes dummy$x but not the attached copy of dummy$x

x # this is the x in the global environment
# [1] 1

rm(x)
x # this is the attached copy of x
#[1] 1 2 3 4 5
dummy$x # this is the x in dummy
# [1] 2 2 2 2 2


> 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."

I'm not following this one...

Best,
Ista
>
>
> 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
>
>
>
>



More information about the R-help mailing list