Pregunta

I am writing a function which plots a linear regression in a subset of my variable.

FitWeibull <- function(data, xinf, xsup){
  xlim <- c(xinf, xsup)
  sub.data <- data
  my.lm <- lm(Y~X, data = sub.data, subset = xlim)
  return(my.lm)
 }

Will this function let me draw my regression between xinf and xsup and keep the other points on the graph?

¿Fue útil?

Solución

You need to specify on what variable you are subsetting. Easiest thing is replacing your sub.data <- data line with: sub.data <- data[(X>xinf & X<xsup),], so that you just have

FitWeibull <- function(data, xinf, xsup){
  sub.data <- data[(data$X > xinf & data$X < xsup),]
  my.lm <- lm(Y~X, data = sub.data)
  return(my.lm)
}

You can then do whatever plotting/summarizing you want with the returned object.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top