2

again I'm stuck...

I want to write a function to get several statistics for checking the assumptions for a linear regression. The function I'm quoting is not yet done, but I think you'll get the point:

check.regression <- function(regmodel, dataframe, resplots = TRUE,
                             durbin = TRUE, savecheck = TRUE) {
     print(dwt(regmodel)) # Durbin-Watson-Test
     dataframe$stand.res <- rstandard(regmodel) # Saving Standardized Residuals                   
}

As you see, I want to save the standardized residuals of the model into the given dataframe.

regmodel refers to the model computed by the linear regression lm( y~x) and dataframe is the name of the dataframe from which the regression model is computed.

The problem is: nothing is saved within my function. If I do the command without the function, the residuals are properly saved into my dataframe.

I guess, there has to be something like

save(dataframe$stand.res <- rstandard(regmodel))

as I also have to specify plotting or writing things to the console within a function, but I don't know how that command might be.

Any ideas?

1 Answer 1

4

R uses pass-by-value so what is sent to the function is a copy of your data.frame. (sort of, passing on some details.)

So when you call the function, you need to 1) return the modified data.frame and 2) assign it or you will lose the results.

check.regression <- function(regmodel, dataframe, resplots = TRUE,
                             durbin = TRUE, savecheck = TRUE) {
     print(dwt(regmodel)) # Durbin-Watson-Test
     dataframe$stand.res <- rstandard(regmodel) # Saving Standardized Residuals
     return(dataframe)                  
}

dataframe <- check.regression(regmodel, dataframe)
Sign up to request clarification or add additional context in comments.

1 Comment

Sorry, now I get it. I overlooked the ende of your reply (dataframe <- check.regression(regmodel, dataframe))! With this command, everything works well. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.