简体   繁体   English

R和RStudio中控制台中的不同显示

[英]Different display in console in R and RStudio

Following the code of a tutorial I defined class and methods for my function of linear regression. 按照教程的代码,我为线性回归函数定义了类和方法。

data(cats, package="MASS")
linmodeEst <- function(x,y){

  qx   <- qr(x) # QR-decomposition
  coef <- solve.qr(qx,y) # solve(t(x)%*%x)%*%t(x)%*%y

  df     <- nrow(x)-ncol(x)
  sigma2 <- sum((y-x%*%coef)^2)/df
  vcov   <- sigma2 * chol2inv(qx$qr)

  colnames(vcov) <- rownames(vcov) <- colnames(x)

  list(coefficients = coef, vcov=vcov, sigma = sqrt(sigma2), df=df)
}

linmod <- function(x,...) UseMethod("linmod")

linmod.default <- function(x,y,...){
  x <- as.matrix(x)
  y <- as.matrix(y)

  est <- linmodeEst(x,y)

  est$fitted.values <- as.vector(x%*%est$coefficients)
  est$residuals     <- y - est$fitted.values
  est$call          <- match.call()
  class(est) <- "linmod"
  est
}

print.linmod <- function(x,...){
  cat("Call:\n")
  print(x$call)
  cat("\nCoefficients:\n")
  print(x$coefficients)
}

summary.linmod <- function(object,...){
  se <- sqrt(diag(object$vcov))
  tval <- coef(object)/se
  TAB <- cbind(Estimate = coef(object),
               StdErr = se,
               t.value = tval,
               p.value = 2*pt(-abs(tval), df=object$df))
  res <- list(call=object$call, coefficients=TAB)
  class(res) <- "summary.linmod"
  res
}

print.summary.linmod <- function(x,...){
  cat("Call:\n")
  print(x$call)
  cat("\n")

  printCoefmat(x$coefficients, P.value=TRUE, has.Pvalue=TRUE)
}
x = cbind(Const=1, Bwt=cats$Bwt)
y = cats$Hw
mod1 <- linmod(x,y)
summary(mod1)

So, in the summary.linmod <- function(object,...) I defined the table names: Estimate, StdErr, t.value, p.value . 因此,在summary.linmod <- function(object,...) Estimate, StdErr, t.value, p.value summary.linmod <- function(object,...)我定义了表名称: Estimate, StdErr, t.value, p.value In RI get all the names in the header, in RStudio just StdErr. 在RI中,获取标题中的所有名称,在RStudio中,仅获取StdErr。 why is this happening? 为什么会这样呢?

My system: Linux 64bit, R 3.1.1 我的系统:Linux 64位,R 3.1.1

在此处输入图片说明

The documentation at ?cbind states that: "For cbind (rbind) the column (row) names are taken from the colnames (rownames) of the arguments if these are matrix-like." ?cbind的文档指出:“对于cbind(rbind),列(行)名称取自参数的列名(行名)(如果它们类似于矩阵)。”

In constructing TAB , you are binding 3 single-column matrices (ie coef(object) , tval , and 2*pt(-abs(tval), df=object$df) , to se (a non-matrix vector with 2 elements). Because of the behaviour quoted above, cbind uses the matrices' names (empty) to name the matrix-like columns of TAB . 在构造TAB ,您将3个单列矩阵(即coef(object)tval2*pt(-abs(tval), df=object$df)se (具有2个元素的非矩阵向量)。由于上述行为, cbind使用矩阵名称(空)来命名TAB的矩阵状列。

Use cbind.data.frame or simply data.frame to construct TAB , and your summary output will have the expected names: 使用cbind.data.frame或者干脆data.frame来构建TAB ,你的总结输出将有预期的名字:

summary.linmod <- function(object,...){
  se <- sqrt(diag(object$vcov))
  tval <- coef(object)/se
  TAB <- data.frame(Estimate = coef(object),
                    StdErr = se,
                    t.value = tval,
                    p.value = 2*pt(-abs(tval), df=object$df))
  res <- list(call=object$call, coefficients=TAB)
  class(res) <- "summary.linmod"
  res
}


> summary(mod1)
# Call:
# linmod.default(x = x, y = y)
# 
#       Estimate   StdErr t.value p.value    
# Const -0.35666  0.69228 -0.5152  0.6072    
# Bwt    4.03406  0.25026 16.1194  <2e-16 ***
# ---
# Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM