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?

有帮助吗?

解决方案

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top