0

I was wondering how to fix this bit of code I have. I am trying to make 11 plots and save the information for the fit for each plot, but when I try to graph the line of best fit, I get the following error:

Error in int_abline(a = a, b = b, h = h, v = v, untf = untf, ...) : 
  invalid a=, b= specification
In addition: Warning message:
In int_abline(a = a, b = b, h = h, v = v, untf = untf, ...) :
  NAs introduced by coercion

Here is my code:

for(n in 2:12){
  nam<-paste("fit", n, sep = "")
  assign(nam, lm(profilin[101:135,n]~profilin[101:135,1]))
  plot(profilin[101:135,1], profilin[101:135,n], ylim=c(200,450), xlab="Time", ylab="Flourecence", col=cl[n-1])
  abline(nam)
  legend("topleft", legend=c(colnames(profilin)[n]), col=c(cl[n-1]), lwd=1)
}

Here is also a bit of my data:

profilin <- structure(list(Time = c(1500L, 1515L, 1530L, 1545L, 1560L, 1575L, 
1590L, 1605L, 1620L, 1635L, 1650L, 1665L, 1680L, 1695L, 1710L
), ActinOnly = c(357.03, 353.37, 350.22, 367.78, 367.1, 361.02, 
374.11, 371.67, 371.84, 379.44, 375.77, 377.25, 372.13, 385.1, 
386.98), p0_5uM = c(341.37, 331.86, 341.82, 341.58, 349.91, 351.02, 
353.87, 354.51, 353.64, 342.91, 355.29, 351, 354.67, 361.42, 
363.92), p1uM = c(336.23, 335.37, 324.01, 347.67, 343.42, 343.42, 
330.55, 358.45, 335.15, 321.5, 342.23, 353.56, 337.57, 360.57, 
350.06)), row.names = 101:115, class = "data.frame")
cl <- rainbow(ncol(profilin)-1)
MrFlick
  • 195,160
  • 17
  • 277
  • 295
waldo_gb
  • 3
  • 2
  • It's generally not a good idea to use `assign()`. It's much easier to work with values in R in a named list. For more specific solutions you should provide a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input that can be used for testing. We don't need your entire data with hundreds of columns, just something reasonably interesting that is similar to your data to test possible solutions. A quick but not recommended fix would be `abline(get(nam))`. Keep in mind that strings behave very differently than variable names – MrFlick Apr 15 '21 at 23:06
  • @MrFlick thank you, I added some data to my question, hopefully that works. – waldo_gb Apr 15 '21 at 23:51
  • What are you trying to do with the plots? Do you wnat them to go to an output device? `pdf()`?, `png()`? – IRTFM Apr 16 '21 at 00:05

1 Answers1

0

It's recommended that you store related values in a list. This makes it much easier to use the values in R and doesn't clutter your global environment with a bunch of variables with indexes in their name. You could use lapply here

fit <- lapply(2:4, function(n) {
  rows <- as.character(101:135)
  model <- lm(profilin[rows,n]~profilin[rows,1])
  plot(profilin[rows,1], profilin[rows,n], ylim=c(200,450), xlab="Time", ylab="Flourecence", col=cl[n-1])
  abline(model)
  legend("topleft", legend=c(colnames(profilin)[n]), col=c(cl[n-1]), lwd=1)
  model # this will be the return value
})

Now fit will be a list of all your models. You could then easily get the number of observations from each model with sapply(fit, nobs) or get all the coefficients with sapply(fit, coef)

MrFlick
  • 195,160
  • 17
  • 277
  • 295
  • Thank you! That worked and everything is a lot cleaner now too. Is there a good way to get the r squared value, because I noticed that summary(fit)$r.squared or sapply(fit, r.squared) do not work. – waldo_gb Apr 16 '21 at 05:33
  • You can use `sapply(fit, function(x) summary(x)$r.squared)` – MrFlick Apr 16 '21 at 06:16