English 中文(简体)
A. 如何在R中进行预见
原标题:How to do introspection in R
  • 时间:2010-03-18 03:05:15
  •  标签:
  • r

我对R有些新之处,而且我有这样一部法典,它会产生一个不了解哪类的变量。 R是否有任何预测设施可以告诉我这种变数属于哪一类?

以下是这一变量的财产:

我正在研究线性模型选择,而我拥有的资源是另一个模型产生的<代码>lm。 现在我想检索<代码>lm。 指挥摘要(模拟)呼吁,以便我无需硬化示范结构。 然而,由于我不得不改变数据集,我需要就“扼杀”作一些修改,但显然这并非简单明了。 我想知道是否有任何指挥类似于扼杀。 取而代之的是,我能够从可变的召唤中操纵这一变量。

> str<-summary(rdnM)$call
> str
lm(formula = y ~ x1, data = rdndat)
> str[1]
lm()
> str[2]
y ~ x1()
> str[3]
rdndat()
> str[3] <- data
Warning message:
In str[3] <- data :
  number of items to replace is not a multiple of replacement length
> str
lm(formula = y ~ x1, data = c(10, 20, 30, 40))
> str<-summary(rdnM)$call
> str
lm(formula = y ~ x1, data = rdndat)
> str[3] <-  data 
> str
lm(formula = y ~ x1, data = "data")
> str<-summary(rdnM)$call
> type str
Error: unexpected symbol in "type str"
> 
最佳回答

In terms of introspection: R allows you to easily examine and operate on language objects.
For more details, see R Language Definition, particularly sections 2 and 6. For instance, in your case, summary(rdnM)$call is a "call" object. You can retrieve pieces of it by indexing, but you can t construct another call object by assigning to indices like you are trying to do. You d have to construct a new call.

在你的情况下,你正在从现有呼吁中发出一条最新的呼吁。 如果您希望重新使用关于不同数据的公式,请通过<代码>formula(foo$ calls)从呼吁标的中提取公式。

 foo <- lm(formula = y ~ x1, data = data.frame(y=rnorm(10),x1=rnorm(10)))
 bar <- lm(formula(foo$call), data = data.frame(y=rnorm(10),x1=rnorm(10)))

另一方面,如果你试图更新该公式,你可使用<编码>更新日期():

baz <- update(bar, . ~ . - 1)
baz$call
##>lm(formula = y ~ x1 - 1, data = data.frame(y = rnorm(10), x1 = rnorm(10)))
问题回答

暂无回答




相关问题
How to plot fitted model over observed time series

This is a really really simple question to which I seem to be entirely unable to get a solution. I would like to do a scatter plot of an observed time series in R, and over this I want to plot the ...

REvolution for R

since the latest Ubuntu release (karmic koala), I noticed that the internal R package advertises on start-up the REvolution package. It seems to be a library collection for high-performance matrix ...

R - capturing elements of R output into text files

I am trying to run an analysis by invoking R through the command line as follows: R --no-save < SampleProgram.R > SampleProgram.opt For example, consider the simple R program below: mydata =...

R statistical package: wrapping GOFrame objects

I m trying to generate GOFrame objects to generate a gene ontology mapping in R for unsupported organisms (see http://www.bioconductor.org/packages/release/bioc/vignettes/GOstats/inst/doc/...

Changing the order of dodged bars in ggplot2 barplot

I have a dataframe df.all and I m plotting it in a bar plot with ggplot2 using the code below. I d like to make it so that the order of the dodged bars is flipped. That is, so that the bars labeled "...

Strange error when using sparse matrices and glmnet

I m getting a weird error when training a glmnet regression. invalid class "dgCMatrix" object: length(Dimnames[[2]]) must match Dim[2] It only happens occasionally, and perhaps only under larger ...

Generating non-duplicate combination pairs in R

Sorry for the non-descriptive title but I don t know whether there s a word for what I m trying to achieve. Let s assume that I have a list of names of different classes like c( 1 , 2 , 3 , 4 ) ...

Per panel smoothing in ggplot2

I m plotting a group of curves, using facet in ggplot2. I d like to have a smoother applied to plots where there are enough points to smooth, but not on plots with very few points. In particular I d ...

热门标签