- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
这是我使用 sourceCpp
获取的 cpp 文件:
#include <RcppArmadillo.h>
// [[Rcpp::depends(RcppArmadillo)]]
using namespace Rcpp;
// [[Rcpp::export]]
List mylm(NumericVector yr, NumericMatrix Xr) {
int n = Xr.nrow(), k = Xr.ncol();
arma::mat X(Xr.begin(), n, k, false); // reuses memory and avoids extra copy
arma::colvec y(yr.begin(), yr.size(), false);
arma::colvec coef = arma::solve(X, y); // fit model y ~ X
arma::colvec resid = y - X*coef; // residuals
double sig2 = arma::as_scalar( arma::trans(resid)*resid/(n-k) );
// std.error of estimate
arma::colvec stderrest = arma::sqrt( sig2 * arma::diagvec( arma::inv(arma::trans(X)*X)) );
return Rcpp::List::create(
Rcpp::Named("coefficients") = coef,
Rcpp::Named("stderr") = stderrest
) ;
}
(代码取自@Romain François 对这个问题的回答:Using `sourceCpp` to compile `fastLm`)
然后在 R 中:
sourceCpp('b.cpp')
set.seed(1)
x = matrix(rnorm(100), 25, 4)
y = rnorm(25)
mylm(y, x)
summary(lm(y~x))
mylm(y, x)
# $coefficients
# [,1]
# [1,] 0.068978
# [2,] 0.117632
# [3,] -0.029917
# [4,] -0.168648
#
# $stderr
# [,1]
# [1,] 0.17970
# [2,] 0.24312
# [3,] 0.15138
# [4,] 0.21266
summary(lm(y~x))
# Call:
# lm(formula = y ~ x)
#
# Residuals:
# Min 1Q Median 3Q Max
# -0.869 -0.487 -0.271 0.410 1.831
#
# Coefficients:
# Estimate Std. Error t value Pr(>|t|)
# (Intercept) 0.1122 0.1718 0.65 0.52
# x1 0.0499 0.1845 0.27 0.79
# x2 0.1076 0.2470 0.44 0.67
# x3 -0.0435 0.1549 -0.28 0.78
# x4 -0.1750 0.2158 -0.81 0.43
#
# Residual standard error: 0.835 on 20 degrees of freedom
# Multiple R-squared: 0.054, Adjusted R-squared: -0.135
# F-statistic: 0.285 on 4 and 20 DF, p-value: 0.884
最佳答案
默认情况下,R 的 lm 使用截距拟合模型,即使您传递的设计矩阵不包含初始列 1。所以你会看到以下是相同的:
lm(y ~ x - 1)
mylm(y, x)
如果您想要“常规”模型,则需要修改您的设计矩阵以使其第一列全为 1:
lm(y ~ x)
mylm(y, cbind(1, x))
将给出相同的结果。
关于c++ - RcppArmadillo faSTLm 结果与 R 的 lm 不同,我做错了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20034737/
我对 lm 的一些令人不安的行为感兴趣函数和相关的predict.lm R 中的函数。splines基础包提供函数bs生成 b 样条展开,然后可用于使用 lm 拟合样条模型,一个通用的线性模型拟合函数
我使用 minpack.lm 包中的 nls.lm 来拟合许多非线性模型。 由于初始参数估计时的奇异梯度矩阵,它经常在 20 次迭代后失败。 问题是当我在失败之前查看迭代(trace = T)时,我可
我有一个稍微进入饱和状态的外部校准曲线。所以我拟合了一个二阶多项式和一个测量样本的数据框,我想知道其中的浓度。 df_calibration=structure(list(dilution = c(0
我试图弄清楚默认 r plot 的残差与拟合图中使用了哪种平滑线对于 lm对象,所以我通过输入 ?plot.lm 查看了帮助页面,因为据我了解 .是如何定义不同对象类型的这些默认行为的。 正如预期的那
我正在尝试使用 R 创建一个线性模型并使用它来预测一些值。主题是棒球数据。如果我这样做: obp <- lm(offense$R ~ offense$OBP) predict(obp, newdata
我有两个变量,我想找到它们之间的相关性。问题是,根据我使用的方法,我似乎得到了不同的结果。 我知道的一种方法是使用 scale() 函数中的自变量和因变量运行 lm() 函数。 所以下面的变量看起来像
我在使用 C Makefile 时遇到了一些问题。 Makefile 的内容如下: PROJECT = 3D-ELM MPICC = mpicc CLAGS = -g -O3 LIBS = -lm S
我使用 caret R 包作为一个非常方便的建模包装器。虽然这是一个奇怪的用法,但在使用模型类型 =“lm”和“无”的交叉验证时,我在从模型中提取结果时遇到了一些麻烦。参见下面的示例: library
我想使用 lm 在 R 中拟合线性模型获得总模型拟合的系数估计值和 p 值 + p 值(类似方差分析),因此基本上来自 summary.lm 的输出. 问题是我想使用我自己的模型矩阵,而不是在调用 l
我建了一个 lm不使用 data= 的模型范围: m1 <- lm( mdldvlp.trim$y ~ gc.pc$scores[,1] + gc.pc$scores[,2] + gc.pc$sco
我是 R 的新手,我只是在学习 apply功能及其工作方式。我只想从 lm 中提取系数适合几年内产品颜色和品牌的变量 x。 我知道我可以创建一个 for 循环并按型号年份对数据进行子集化并拟合它,但我
如何计算 df 中存储在列中的多个变量的行向 lm()/系数? 我有这种数据(只是例子): set.seed(1) foo trialNumber Nr1 Nr2
我对在 ggplot2 中自动绘制模型很感兴趣。基于 discussion在 ggplot2 问题跟踪器上,我相信像下面这样的包装器应该可以工作。 geom_predict Warning: Com
我正在对多个属性(包括两个分类属性B和F)进行线性回归,但是我没有获得每个系数水平的系数值。 B具有9个级别,而F具有6个级别。当我最初运行模型(带有截距)时,我得到了8个B系数和5个F系数,我将其理
我一直试图弄清楚 subset R 中的参数 lm()功能有效。特别是以下代码对我来说似乎很可疑: data(mtcars) summary(lm(mpg ~ wt, data=mtcars))
我有以下数据框 > df df2 Economy ConditionA ConditionB ConditionC ConditionD 1 FRANCE 9
我正在使用来自包鼠标的男孩数据集的数据。当我对其中一个因子变量 (phb) 运行回归时,输出会显示这些因子,但给它们的名称与数据中的名称不同。我想知道为什么会这样。有没有办法纠正它? library(
通常,我和你(假设你不是机器人)很容易识别预测器是分类的还是定量的。例如,性别显然是分类的。您的最后一票可以分类。 基本上,我们可以轻松识别分类预测变量。但是当我们在 R 中输入一些数据时会发生什么,
我们从中得到了一个 lm 对象并想提取标准错误 lm_aaa<- lm(aaa~x+y+z) 我知道函数摘要、名称和系数。 但是,摘要似乎是手动访问标准错误的唯一方法。 你知道我怎么能输出se吗? 谢
我正在拟合一个模型来分解数据并进行预测。如果newdata中的predict.lm()包含模型未知的单个因子级别,则所有predict.lm()都会失败并返回错误。 有没有一种好方法可以让predic
我是一名优秀的程序员,十分优秀!