Model formula for ols function (rms package)

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Model formula for ols function (rms package)

Mark Seeto
Dear R help,

I'm having some trouble with model formulas for the ols function in
the rms package. I want to have two variables represented as
restricted cubic splines, and also include an interaction as a product
of linear terms, but I get an error message.

library(rms)
d <- data.frame(x1 = rnorm(50), x2 = rnorm(50), y = rnorm(50))

ols(y ~ rcs(x1,3) + rcs(x2,3) + x1*x2, data=d)
Error in if (!length(fname) || !any(fname == zname)) { :
  missing value where TRUE/FALSE needed

ols(y ~ rcs(x1,3) + rcs(x2,3) + I(x1*x2), data=d)
Error in if (!length(fname) || !any(fname == zname)) { :
  missing value where TRUE/FALSE needed

I get the same error if I try to fit a model with a quadratic term:

ols(y ~ x1 + I(x1^2), data=d)
Error in if (!length(fname) || !any(fname == zname)) { :
  missing value where TRUE/FALSE needed

ols(y ~ I(x1^2), data=d)  # No error message, but lacks linear term

Is there a way to do these things without first creating new variables
in the data frame?

Thanks,

Mark Seeto
National Acoustic Laboratories

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: Model formula for ols function (rms package)

Frank Harrell
For the first example you want the restricted interaction operator: y ~ rcs(x1, 3) + rcs(x2, 3) + rcs(x1, 3) %ia% rcs(x2, 3).

For the second example use pol(x,2) or something like pol(x1,2) + pol(x2,2) + pol(x1, 2) %ia% pol(x2, 2)

If you have to create new variables for R formulas you're usually doing something wrong.
Frank

Mark Seeto wrote
Dear R help,

I'm having some trouble with model formulas for the ols function in
the rms package. I want to have two variables represented as
restricted cubic splines, and also include an interaction as a product
of linear terms, but I get an error message.

library(rms)
d <- data.frame(x1 = rnorm(50), x2 = rnorm(50), y = rnorm(50))

ols(y ~ rcs(x1,3) + rcs(x2,3) + x1*x2, data=d)
Error in if (!length(fname) || !any(fname == zname)) { :
  missing value where TRUE/FALSE needed

ols(y ~ rcs(x1,3) + rcs(x2,3) + I(x1*x2), data=d)
Error in if (!length(fname) || !any(fname == zname)) { :
  missing value where TRUE/FALSE needed

I get the same error if I try to fit a model with a quadratic term:

ols(y ~ x1 + I(x1^2), data=d)
Error in if (!length(fname) || !any(fname == zname)) { :
  missing value where TRUE/FALSE needed

ols(y ~ I(x1^2), data=d)  # No error message, but lacks linear term

Is there a way to do these things without first creating new variables
in the data frame?

Thanks,

Mark Seeto
National Acoustic Laboratories

______________________________________________
[hidden email] 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.
Frank Harrell
Department of Biostatistics, Vanderbilt University
Reply | Threaded
Open this post in threaded view
|

Re: Model formula for ols function (rms package)

Mark Seeto
In reply to this post by Mark Seeto
Thank you for your reply, Frank. %ia% drops the product x1'*x2', but
keeps x1*x2' and x1'*x2. Is there a way to drop all three of these and
include only x1*x2? Or is this not a sensible thing to want to do?

Thanks,
Mark


Frank E Harrell Jr <[hidden email]> wrote:
For the first example you want the restricted interaction operator: y
~ rcs(x1, 3) + rcs(x2, 3) + rcs(x1, 3) %ia% rcs(x2, 3).

For the second example use pol(x,2) or something like pol(x1,2) +
pol(x2,2) + pol(x1, 2) %ia% pol(x2, 2)

If you have to create new variables for R formulas you're usually
doing something wrong.
Frank

Mark Seeto wrote:
Dear R help,

I'm having some trouble with model formulas for the ols function in
the rms package. I want to have two variables represented as
restricted cubic splines, and also include an interaction as a product
of linear terms, but I get an error message.

library(rms)
d <- data.frame(x1 = rnorm(50), x2 = rnorm(50), y = rnorm(50))

ols(y ~ rcs(x1,3) + rcs(x2,3) + x1*x2, data=d)
Error in if (!length(fname) || !any(fname == zname)) { :
  missing value where TRUE/FALSE needed

ols(y ~ rcs(x1,3) + rcs(x2,3) + I(x1*x2), data=d)
Error in if (!length(fname) || !any(fname == zname)) { :
  missing value where TRUE/FALSE needed

I get the same error if I try to fit a model with a quadratic term:

ols(y ~ x1 + I(x1^2), data=d)
Error in if (!length(fname) || !any(fname == zname)) { :
  missing value where TRUE/FALSE needed

ols(y ~ I(x1^2), data=d)  # No error message, but lacks linear term

Is there a way to do these things without first creating new variables
in the data frame?

Thanks,

Mark Seeto
National Acoustic Laboratories

______________________________________________
[hidden email] 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.
«  [hide part of quote]

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: Model formula for ols function (rms package)

Frank Harrell
Hi Mark,

You are quite correct.  My apologies.  That should have been

rcs(x1, 3) + rcs(x2, 3) + x1 %ia% x2
or
pol(x1, 2) + pol(x2, 2) + x1 %ia% x2

Frank

Mark Seeto wrote
Thank you for your reply, Frank. %ia% drops the product x1'*x2', but
keeps x1*x2' and x1'*x2. Is there a way to drop all three of these and
include only x1*x2? Or is this not a sensible thing to want to do?

Thanks,
Mark


Frank E Harrell Jr <[hidden email]> wrote:
For the first example you want the restricted interaction operator: y
~ rcs(x1, 3) + rcs(x2, 3) + rcs(x1, 3) %ia% rcs(x2, 3).

For the second example use pol(x,2) or something like pol(x1,2) +
pol(x2,2) + pol(x1, 2) %ia% pol(x2, 2)

If you have to create new variables for R formulas you're usually
doing something wrong.
Frank

Mark Seeto wrote:
Dear R help,

I'm having some trouble with model formulas for the ols function in
the rms package. I want to have two variables represented as
restricted cubic splines, and also include an interaction as a product
of linear terms, but I get an error message.

library(rms)
d <- data.frame(x1 = rnorm(50), x2 = rnorm(50), y = rnorm(50))

ols(y ~ rcs(x1,3) + rcs(x2,3) + x1*x2, data=d)
Error in if (!length(fname) || !any(fname == zname)) { :
  missing value where TRUE/FALSE needed

ols(y ~ rcs(x1,3) + rcs(x2,3) + I(x1*x2), data=d)
Error in if (!length(fname) || !any(fname == zname)) { :
  missing value where TRUE/FALSE needed

I get the same error if I try to fit a model with a quadratic term:

ols(y ~ x1 + I(x1^2), data=d)
Error in if (!length(fname) || !any(fname == zname)) { :
  missing value where TRUE/FALSE needed

ols(y ~ I(x1^2), data=d)  # No error message, but lacks linear term

Is there a way to do these things without first creating new variables
in the data frame?

Thanks,

Mark Seeto
National Acoustic Laboratories

______________________________________________
[hidden email] 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.
«  [hide part of quote]

______________________________________________
[hidden email] 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.
Frank Harrell
Department of Biostatistics, Vanderbilt University