consider for example the "iris" dataframe which is installed with main setup of R :
names(iris) # [1] "Sepal.Length" "Sepal.Width" "Petal.Length" "Petal.Width" "Species" levels(iris$Species) # [1] "setosa" "versicolor" "virginica" now I construct three models without attaching the "iris":
t1=lm(iris$Sepal.Length ~ iris$Sepal.Width + iris$Petal.Length , data=iris) t2=lm(iris$Sepal.Length ~ iris$Sepal.Width + iris$Petal.Length , data=iris[iris$Species=="setosa",]) t3=lm(iris$Sepal.Length ~ iris$Sepal.Width + iris$Petal.Length , data=iris , subset = (iris$Species=="setosa")) now i think t2=t3<>t1 but R says t1=t2<>t3. why I'm wrong?!!
now I construct again my models but this time with attaching the "iris":
attach(iris) t1=lm(Sepal.Length ~ Sepal.Width + Petal.Length , data=iris) t2=lm(Sepal.Length ~ Sepal.Width + Petal.Length , data=iris[iris$Species=="setosa",]) t3=lm(Sepal.Length ~ Sepal.Width + Petal.Length , data=iris , subset = (iris$Species=="setosa")) now me and R both think: t2=t3<>t1. but again I'm confused because of the effect of attaching on model! I think first set of models is equivalent to second set of models, but R says no! thanks.
tm1=lm(Sepal.Length ~ Sepal.Width + Petal.Length, data=iris)works.setosa <- subset(iris, subset = Species == "setosa")then you can run the linear model on the subset.attach(), it just leads to bad habits.iris$is what is actually changing the model fort2. Basically, you are trying to give a subset to thedataargument, then overwriting that when you useiris$. Just remove theiris$fromt2in the first block and it will match the other subsetted output.