Question

I have a data list and I want to do a lm with two variable in the list. I made this function to plot the two linear regressions. Here is my code:

FitWeibull <- function(data){

my.lm1 <- lm(data[[1]]$X ~ data[[1]]$Y, data = data)
my.lm2 <- lm(data[[2]]$X ~ data[[2]]$Y, data = data)
#   return(list(my.lm1, my.lm2))

return(list(abline(my.lm1), abline(my.lm2)))
}

FitWeibull(my.data)

[[1]]
NULL

[[2]]
NULL

Even with just one regression, it doesn't work:

FitWeibull <- function(data){

my.lm1 <- lm(data[[1]]$X ~ data[[1]]$Y, data = data)
#   my.lm2 <- lm(data[[2]]$X ~ data[[2]]$Y, data = data)
#   return(list(my.lm1, my.lm2))
return(abline(my.lm1))
}

FitWeibull(my.data) returns anything

Can you help me? Thanks!

Was it helpful?

Solution

Try this

## Create some data set
set.seed(1)
my.data <- list(df1 = data.frame(Y = sample(10), X = sample(10)), 
             df2 = data.frame(Y = sample(10), X = sample(10)))

FitWeibull <- function(data){

  my.lm1 <- lm(data[[1]]$X ~ data[[1]]$Y)
  my.lm2 <- lm(data[[2]]$X ~ data[[2]]$Y)

  par(mfrow = c(1, 2), pty = "s")  

  plot(data[[1]]$X , data[[1]]$Y)
  abline(my.lm1)

  plot(data[[2]]$X , data[[2]]$Y)
  abline(my.lm2)

}
FitWeibull(my.data)

enter image description here

OTHER TIPS

The function abline doesn't return anything. It just adds lines to a graphics device. Then you can't return it. You can return the my.lm1 and my.lm2 objects if you like and call abline on them later.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top