1

I'm working with a data frame (dt) that contains 3 columns: Time, Temp, Species, such as

 Temp Time    Species
 1   10   241 Species-X
 2   11   241 Species-X
 3   12   241 Species-X
 4   13   241 Species-Y
 5   14   241 Species-Y
 6   15   240 Species-Z
...  ...  ...     ...
 41  50   178 Species-Z

There are five species in the third column. I want to apply a linear model (lm) with Temp as my independent variable and Time as the dependent variable. So I want to test it just for species X, or Y. Further, I want to test for one of my species at a given Temp interval (let's say 20 - 29 Degrees C). I have tried:

lm(Temp ~ Time, data = td[Species =  Species-Y])

for(i in unique(td$Species)){
model <- list(model)
model[i] <- lm(td$Time ~ td$Temp)
}

model <- function (dados) {
    return(lm(td$Time[,dados] ~ td$Temp[,dados]))
  }
model(dados = td$Species-X)
Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
  • http://www.statmethods.net/management/subset.html – Rorschach Aug 06 '15 at 22:49
  • 2
    I would use the `subset` feature of `lm`... `lapply(c("Species-X", "Species-Y"), function(x) lm(Time ~ Temp, data=td, subset=Species==x))` – user20650 Aug 06 '15 at 22:53
  • Your first try above nearly works ... you need to use quotes, and need an extra comma. So try `lm(Temp ~ Time, data = td[td$Species == "Species-Y" , ])` – user20650 Aug 06 '15 at 23:00

1 Answers1

2

The by() function in base R provides one solution:

by(td, td$Species, function(df) lm(df[,"Temp"] ~ df[,"Time"]))

If you want different rules for different subsets, you're probably going to need to do it group by group. For example, to do Species-Y for temps of 20-29 (inclusive), you could run:

lm(Temp ~ Time, data = td[td$Species == "Species-Y" & td$Temp >= 20 & td$Temp <= 29, ])
ulfelder
  • 5,305
  • 1
  • 22
  • 40
  • thank you fo your answer, but I could not manage to make it work. I can`t figure out what I am doing wrong... i keep getting these Warning messages: 1: In model.response(mf, "numeric") : using type = "numeric" with a factor response will be ignored 2: In Ops.factor(y, z$residuals) : ‘-’ not meaningful for factors ... But I`ll keep on trying – Marcos Patrício Aug 06 '15 at 23:56
  • Whichever variable you're using as the `y` in your call to `lm` --- and from your post, I'm not sure which one that is --- must be encoded as a factor. Let's assume it's `time`. Before you run the `by()` line, run `td$Time <- as.numeric(td$Time)` and then try it. – ulfelder Aug 07 '15 at 01:39
  • 1
    it worked! I owe you big time ufelder! – Marcos Patrício Aug 07 '15 at 01:48
  • 1
    If it worked, it would be helpful to officially accept the answer. – ulfelder Aug 07 '15 at 02:00
  • Hi ulfelder, I did upvote your answer, but I'm new here and the site informed me that i need 15 evaluations before my votes begin to be counted. If there's a different way I can officially accept your answer, just say it – Marcos Patrício Aug 08 '15 at 01:38