Hello!

Here is my data:
x<-data.frame(y=rnorm(100,0,1),a=rnorm(100,1,1),b=rnorm(100,2,1),weights=runif(100))
data.for.regression<-x[1:3]
names(data.for.regression)
myweights<-x$weights

I run simple weighted regression and everything is fine:
reg1<-lm(y~., data.for.regression, weights=myweights)

Then I defined a simple function for the same regression - that adds a
couple more statistics to the output - see function "weighted.reg"
below.
I run this function and try to use it:
weighted.reg(y~., MyData=x, weights.vector=myweights, file.name="TEST.csv")

And I get the following error: "Error in eval(expr, envir, enclos) :
object 'my.weights' not found"
I tried everything and still don't understand - why can't it find "my.weights"?
Thanks a lot for your help!


weighted.reg = function(formula, MyData, weights.vector, file.name)
{
        library(lmtest)
        library(car)
        my.weights<-weights.vector
        regr.f<-lm(formula,MyData,weights=my.weights,na.action=na.omit)

        results<-as.data.frame(round(summary(regr.f)$coeff,3))
        results$VIF<-c(NA,round(vif(regr.f),2))
        results[nrow(results)+1,1]<-round(summary(regr.f)$r.squared,3)
        results[nrow(results)+1,1]<-round(summary(regr.f)$adj.r.squared,3)
        results[nrow(results)+1,1]<-round(dwtest(regr.f)$statistic,2)
        row.names(results)[(nrow(results)-2):nrow(results)]<-c("R.Sqr",
"Adj.R.Sqr","DW")

        write.csv(results,file=file.name)
        return(results)
}

-- 
Dimitri Liakhovitski
Ninah Consulting
www.ninah.com

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to