簡體   English   中英

將數值的小數位數設置為lm模型的摘要

[英]Set the number of decimals of numeric values to the summary of lm model

我有一個像下面這樣的數據框

combinedata<-structure(list(`Stock Price` = c(92.160004, 96.2699909354283, 
98.3718279614214, 103.135324759444, 101.242020126014, 102.081467809165, 
100.317258285167, 100.844493655917, 96.0234994361343, 98.739287127773, 
92.4398236570887, 95.0714048372593, 89.8987717434699, 91.0505220439386, 
88.0680822806702, 90.5236309606781, 88.2753334722473, 86.9160779976273, 
82.7478740231451, 78.6005699924926, 78.3975546658477, 87.2442756910475, 
86.1054968450499, 82.9100404857697, 82.8036965709299, 85.2500369716081, 
89.7158760697461, 94.8783752588242, 95.5438462896501, 101.455256840256, 
96.2710237978968, 94.3120717241021, 95.2691288602746, 90.9466601032546, 
95.2909727726717, 99.3943053646555, 92.3796565962705, 89.5495654581188, 
91.135212533312, 90.7351561145736, 89.4571835297895, 90.5599744560346, 
89.7859413958874, 85.6241914008189, 92.853530228177, 94.40523394334, 
94.3372837276363, 95.6104715629249, 99.7942743005071, 86.57982764559, 
86.1602329118228, 89.786323780763, 93.8165827641153, 96.5056289127173, 
95.0824714224238, 93.519334258378, 100.199895759649, 93.9064615530742, 
93.6943226079577, 81.179671317758, 87.2392708233211), `Market Index Return` = c(15698.849609, 
16321.709961, 16457.660156, 16580.839844, 16717.169922, 16826.599609, 
16563.300781, 17098.449219, 17042.900391, 17390.519531, 17828.240234, 
17823.070313, 17164.949219, 18132.699219, 17776.119141, 17840.519531, 
18010.679688, 17619.509766, 17689.859375, 16528.029297, 16284.700195, 
17663.539063, 17719.919922, 17425.029297, 16466.300781, 16516.5, 
17685.089844, 17773.640625, 17787.199219, 17929.990234, 18432.240234, 
18400.880859, 18308.150391, 18142.419922, 19123.580078, 19762.599609, 
19864.089844, 20812.240234, 20663.220703, 20940.509766, 21008.650391, 
21349.630859, 21891.119141, 21948.099609, 22405.089844, 23377.240234, 
24272.349609, 24719.220703, 26149.390625, 25029.199219, 24103.109375, 
24163.150391, 24415.839844, 24271.410156, 25415.189453, 25964.820313, 
26458.310547, 25115.759766, 25538.460938, 23327.460938, 24999.669922
)), row.names = c(NA, -61L), class = c("tbl_df", "tbl", "data.frame"
))

並且我應用lm()並且還對每個summary(fit)數值使用最多4 位小數但我得到non-numeric argument to binary operator

fit <- lm(formula = `Stock Price` ~ `Market Index Return`, data = combinedata)

Call:
lm(formula = `Stock Return` ~ MarketIndexReturn, data = combineddata2)

Residuals:
      Min        1Q    Median        3Q       Max 
-0.096002 -0.009879  0.000482  0.008725  0.164773 

Coefficients:
                  Estimate Std. Error t value Pr(>|t|)  
(Intercept)       0.001283   0.001328   0.966   0.3348  
MarketIndexReturn 0.298332   0.166953   1.787   0.0752 .
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 0.02077 on 248 degrees of freedom
Multiple R-squared:  0.01271,   Adjusted R-squared:  0.008731 
F-statistic: 3.193 on 1 and 248 DF,  p-value: 0.07517

我正在嘗試類似的東西:

fit$coefficients<-format(round(fit$coefficients, 4), nsmall = 4)
fit$residuals<-format(round(fit$residuals, 4), nsmall = 4)
summary(fit)

您必須在對象summary返回中進行更改。 首先考慮在options()使用scipen=digits=參數。 它不會做你想要什么,但它可能是足夠接近。 如果您想要正好四位數字,請嘗試:

summ <- summary(fit)
summ$coefficients <- round(summ$coefficients, 4)
summ$residuals <- round(summ$residuals, 4)
summ
#  
# Call:
# lm(formula = `Stock Price` ~ `Market Index Return`, data = combinedata)
# 
# Residuals:
#      Min       1Q   Median       3Q      Max 
# -13.5012  -3.7520   0.4261   3.5332  11.2145 
# 
# Coefficients:
#                       Estimate Std. Error t value Pr(>|t|)    
# (Intercept)            90.6855     4.6288   19.59   <2e-16 ***
# `Market Index Return`   0.0001     0.0002    0.33     0.75    
# ---
# Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
# 
# Residual standard error: 5.89 on 59 degrees of freedom
# Multiple R-squared:  0.00179, Adjusted R-squared:  -0.0151 
# F-statistic: 0.106 on 1 and 59 DF,  p-value: 0.746

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM