2014-09-02 94 views
2

我具有R代码,我需要翻译到Matlab的如下:Matlab的等效R代码:如何计算线性模型的残差值?

xt = c(-0.227, -0.604, 0.974, 2.639, -0.271, -0.355, -0.551, 0.342, 2.390, -1.257) 
sets = c(1, 2, 3, 4, 5, 1, 2, 3, 4, 5) 
methods = c(1, 1, 1, 1, 1, 2, 2, 2, 2, 2) 
wt = c(1, 1, 1, 1, 1, 3, 3, 3, 3, 3) 

sets = as.factor(sets) 
methods = as.factor(methods) 
lm1 <- lm(xt ~ sets + methods, weights = wt) 

我所需要的残余值的线性模型,即是。

lm1$residual 

该polyfit函数不包括权重! Matlab中的什么函数会给我线性模型的剩余值?

+1

所以你想残差加权线性回归?你有统计工具箱吗?因为'fitlm',现在是基本的线性回归工具,将权重作为可选参数,并且明确输出残差。你有什么工具箱和什么版本的Matlab? – Dan 2014-09-02 14:32:59

回答

3

我打算假设你有MATLAB中的统计工具箱。如果你不这样做,那么这是行不通的。


MATLAB中的等效代码几乎是一样的R。您所要做的就是设置一个包含变量的数据框,然后使用fitlmLinearModel.fit来适合您的线性模型。 fitlmLinearModel.fit的最新版本,可从R2013b及更高版本获取。如果您的MATLAB版本晚于此版本,建议您使用fitlm。如果你不这样做,那就用LinearModel.fitlm in R将线性模型拟合到预测变量和输出中,而fitlm/LinearModel.fit在MATLAB中做同样的事情。

你需要做的是像上面所做的那样定义你的变量,但是确保你使用MATLAB中的dataset函数将它们封装在数据框中。之后,通过使用MATLAB中的nominal函数创建因子变量。然后创建线性模型,但指定一个附加标记Weights,以使用您的wt变量来加权每个预测变量和输出组合。一旦创建了线性模型,您只需通过Residuals访问残差字段。您可以在R(也称为Wilkinson notation)中以相同方式定义预测变量和输出变量之间的输入/输出关系。

我需要指出的一点需要注意的是,你需要必须确保你的数据是在列中而不是在行中。您会看到我正在放入数据,但使用转置运算符来确保数据在列中。因此:

% // Define data 
xt = [-0.227, -0.604, 0.974, 2.639, -0.271, -0.355, -0.551, 0.342, 2.390, -1.257].'; 
sets = [1, 2, 3, 4, 5, 1, 2, 3, 4, 5].'; 
methods = [1, 1, 1, 1, 1, 2, 2, 2, 2, 2].'; 
wt = [1, 1, 1, 1, 1, 3, 3, 3, 3, 3].'; 

%// Create data frame and make categorical data 
data = dataset(xt, sets, methods); 
data.sets = nominal(data.sets); 
data.methods = nominal(data.methods); 

%// Create linear model and specify weights 
fit = LinearModel.fit(data, 'xt ~ sets + methods', 'Weights', wt); 
%// or 
%// fit = fitlm(data, 'xt ~ sets + methods', 'Weights', wt); 

%// Access residuals 
res = fit.Residuals; 

这是线性模型,我得到:

fit = 


Linear regression model: 
    xt ~ 1 + sets + methods 

Estimated Coefficients: 
        Estimate SE   tStat  pValue  
    (Intercept)  -0.0317 0.22889 -0.13849  0.89654 
    sets_2   -0.24125 0.25591 -0.94273  0.3992 
    sets_3   0.823 0.25591  3.216  0.032403 
    sets_4   2.7752 0.25591  10.845 0.00041025 
    sets_5   -0.6875 0.25591  -2.6865  0.054855 
    methods_2  -0.3884 0.18689  -2.0783  0.10623 

Number of observations: 10, Error degrees of freedom: 4 
Root Mean Squared Error: 0.362 
R-squared: 0.983, Adjusted R-Squared 0.962 
F-statistic vs. constant model: 46.6, p-value = 0.0

这些残差我得到:

res = 

    Raw   Pearson  Studentized Standardized 
    -0.1953 -0.53964 -0.64365  -0.69667  
    -0.33105 -0.91474  -1.2672  -1.1809  
     0.1827  0.50483  0.597  0.65173  
    -0.10455 -0.28889 -0.32875  -0.37295  
     0.4482  1.2384  2.3047   1.5988  
     0.0651  0.17988  0.37161  0.40223  
    0.11035  0.30491  0.73161  0.68181  
    -0.0609 -0.16828 -0.34468  -0.37628  
    0.03485 0.096296  0.1898  0.21532  
    -0.1494 -0.41281  -1.3306  -0.92308 

菊ST是自包含的,这是我从你的代码获得R,我们应该看到输出或多或少是相同的:

> summary(lm1) 

lm(formula = xt ~ sets + methods, weights = wt) 

Weighted Residuals: 
     1  2  3  4  5  6  7  8  9  10 
-0.19530 -0.33105 0.18270 -0.10455 0.44820 0.11276 0.19113 -0.10548 0.06036 -0.25877 

Coefficients: 
      Estimate Std. Error t value Pr(>|t|)  
(Intercept) -0.0317  0.2289 -0.138 0.89654  
sets2  -0.2412  0.2559 -0.943 0.39920  
sets3   0.8230  0.2559 3.216 0.03240 * 
sets4   2.7753  0.2559 10.845 0.00041 *** 
sets5  -0.6875  0.2559 -2.687 0.05486 . 
methods2  -0.3884  0.1869 -2.078 0.10623  
--- 
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 

Residual standard error: 0.3619 on 4 degrees of freedom 
Multiple R-squared: 0.9831, Adjusted R-squared: 0.962 
F-statistic: 46.58 on 5 and 4 DF, p-value: 0.001226 

> lm1$residuals 

     1  2  3  4  5  6  7  8  9  10 
-0.19530 -0.33105 0.18270 -0.10455 0.44820 0.06510 0.11035 -0.06090 0.03485 -0.14940 

R显示原料残差和这对应于MATLAB中Residuals矩阵的第一列。请注意,残差仍然封装在数据框(dataset类)中。如果要提取数值,可以使用dataset2struct将数据集的每列转换为结构中的字段。这样,您只需使用点符号访问每个列。

如果使用LinearModel.fit,残差数据帧将返回为dataset类型。但是,如果您使用fitlm,则输出实际上是table。在这种情况下,您需要使用table2struct将残差转换为具有相关字段的结构。

换句话说,你会做这样的事情:

resMatrix = dataset2struct(res); %// If using LinearModel.fit 
%// or 
%// resMatrix = table2struct(res); %// If using fitlm 

这就是我得到:

raw = resMatrix.Raw; 
pear = resMatrix.Pearson; 
stu = resMatrix.Studentized; 
sta = resMatrix.Standardized; 

或者:

resMatrix = 

10x1 struct array with fields: 

    Raw 
    Pearson 
    Studentized 
    Standardized 

然后,您可以访问每一列如果要提取原始2D矩阵(如),则可以将输出投射为)。如果你这样说,这是你会得到什么:

resMatrix = double(res) 

resMatrix = 

    -0.1953 -0.5396 -0.6437 -0.6967 
    -0.3311 -0.9147 -1.2672 -1.1809 
    0.1827 0.5048 0.5970 0.6517 
    -0.1046 -0.2889 -0.3288 -0.3730 
    0.4482 1.2384 2.3047 1.5988 
    0.0651 0.1799 0.3716 0.4022 
    0.1103 0.3049 0.7316 0.6818 
    -0.0609 -0.1683 -0.3447 -0.3763 
    0.0349 0.0963 0.1898 0.2153 
    -0.1494 -0.4128 -1.3306 -0.9231 

现在这是一个实际的二维矩阵,你可以访问单个元素,并且可以进行切片操作,过滤操作等,以你的心脏的内容。在你的情况下,你需要原始的残差,所以你会这样做raw = resMatrix(:,1);

+0

对于'table'类型的输入参数,我得到'Undefined function'dataset2struct''。错误,当我运行resMatrix = dataset2struct(res); – user2333346 2014-09-02 18:30:06

+1

@ user2333346 - 这是因为你正在'dataset2struct'中使用'table'。使用'table2struct'代替:http://www.mathworks.com/help/matlab/ref/table2struct.html – rayryeng 2014-09-02 18:33:23

+0

我不知道如何改变它,但你的意思是上面的数据集NOT数据集!那是对的吗? – user2333346 2014-09-02 18:45:58

相关问题