rockon8765

  • 2022年11月1日
  • 注册于 2020年9月22日
  • 自己頂一下...有沒有高手可以提點這篇沒人回的原因

    是發問格式不對?還是問題太簡單?

    感激不盡

    • 大家好

      使用stargazer做比較模型表格時,在程式內看表格很正常,可以清楚看出4個模型的比較

      但knit成pdf後的表格(如下圖),4個模型的間距蠻大的所以有換行,導致閱讀不便

      不知道有沒有辦法調整?

      以下是code和資料

      title: "Statistical Methods in Finance"
      geometry: margin=1.18in
      documentclass: ctexart
      output: 
        bookdown::pdf_book: 
          number_sections: yes
          pandoc_args: --listings
          toc: yes
          fig_crop: no
          base_format: rmarkdown::pdf_document
          latex_engine: xelatex
          citation_package: natbib
          dev: "cairo_pdf"
          template: null
          extra_dependencies:
            sourcecodepro:
             - scale=0.85
            sourceserifpro:
             - rmdefault
            sourcesanspro:
             - sfdefault
            awesomebox:
          quote_footer: ["\\hspace*{\\fill} ", ""]
          geometry:
            - top=30mm
            - left=20mm
          includes:
            in_header: header.tex
      biblio-style: plainnat #apalike
      natbiboptions: "authoryear,round"
      link-citations: yes
      colorlinks: yes
      classoption: "UTF8,a4paper,fontset=adobe,zihao=false"
      {r global_options, include = FALSE}
      knitr::opts_chunk$set(
        fig.width = 7,
        fig.height = 6,
        echo = TRUE,
        warning = FALSE,
        message = FALSE
      )
      tidy = TRUE
      tidy.opts = list(keep.blank.line = FALSE, width.cutoff = 100)
      options(width = 60)
      cex.val = 1 # 加在plot裡面的文字大小
      warnings('off')
      require(dplyr)
      require(tidyverse)
      require(magrittr)
      require(readr)
      require(plm)
      require(ggplot2)
      require(stargazer)
      
      data <- read.csv("C:\\Users\\Yoga\\Desktop\\dairy.csv"
                       , header = T)[, c('FARM', 'YEAR', 'MILK', 'COWS', 'LAND' ,'LABOR', 'FEED')]
      
      pdata=pdata.frame(data,index = c('FARM','YEAR'))
      pool=log(MILK) ~ COWS + LAND + LABOR + FEED
      poolmodel<-plm(pool, data=pdata, model='pooling')
      indmodel<-plm(pool, model='within', effect='individual',data = pdata)
      timemodel<-plm(pool, model='within', effect='time',data = pdata)
      bothmodel<-plm(pool, model='within', effect='twoways',data = pdata)
      stargazer(poolmodel,indmodel,timemodel,bothmodel,type='text',
                column.labels = c("Pooled OLS","FE-individual","FE-time","FE-two-ways"))
      > sessionInfo()
      R version 4.0.3 (2020-10-10)
      Platform: x86_64-w64-mingw32/x64 (64-bit)
      Running under: Windows 10 x64 (build 19042)
      
      Matrix products: default
      
      locale:
      [1] LC_COLLATE=Chinese (Traditional)_Taiwan.950  LC_CTYPE=Chinese (Traditional)_Taiwan.950   
      [3] LC_MONETARY=Chinese (Traditional)_Taiwan.950 LC_NUMERIC=C                                
      [5] LC_TIME=Chinese (Traditional)_Taiwan.950    
      
      attached base packages:
      [1] stats     graphics  grDevices utils     datasets  methods   base     
      
      other attached packages:
       [1] stargazer_5.2.2 plm_2.2-5       magrittr_2.0.1  forcats_0.5.0   stringr_1.4.0   purrr_0.3.4    
       [7] readr_1.4.0     tidyr_1.1.2     tibble_3.0.4    ggplot2_3.3.2   tidyverse_1.3.0 dplyr_1.0.2    
      
      loaded via a namespace (and not attached):
       [1] Rcpp_1.0.5        bdsmatrix_1.3-4   lubridate_1.7.9.2 lattice_0.20-41   ps_1.5.0          zoo_1.8-8        
       [7] assertthat_0.2.1  digest_0.6.27     lmtest_0.9-38     R6_2.5.0          cellranger_1.1.0  backports_1.2.0  
      [13] reprex_0.3.0      evaluate_0.14     httr_1.4.2        pillar_1.4.7      Rdpack_2.1        miscTools_0.6-26 
      [19] rlang_0.4.9       readxl_1.3.1      rstudioapi_0.13   callr_3.5.1       whisker_0.4       Matrix_1.2-18    
      [25] rmarkdown_2.5     labeling_0.4.2    splines_4.0.3     munsell_0.5.0     broom_0.7.2       compiler_4.0.3   
      [31] modelr_0.1.8      xfun_0.19         pkgconfig_2.0.3   clipr_0.7.1       mgcv_1.8-33       maxLik_1.4-6     
      [37] htmltools_0.5.0   tidyselect_1.1.0  fansi_0.4.1       crayon_1.3.4      dbplyr_2.0.0      withr_2.3.0      
      [43] MASS_7.3-53       rbibutils_2.0     grid_4.0.3        nlme_3.1-149      jsonlite_1.7.1    gtable_0.3.0     
      [49] lifecycle_0.2.0   DBI_1.1.0         scales_1.1.1      cli_2.2.0         stringi_1.5.3     farver_2.0.3     
      [55] fs_1.5.0          xml2_1.3.2        ellipsis_0.3.1    generics_0.1.0    vctrs_0.3.5       sandwich_3.0-0   
      [61] Formula_1.2-4     tools_4.0.3       glue_1.4.2        hms_0.5.3         processx_3.4.5    yaml_2.2.1       
      [67] colorspace_2.0-0  gbRd_0.4-11       rvest_0.3.6       knitr_1.30        haven_2.3.1 

      使用reprex()時出現error,所以改提供資料連結,還請大家見諒

      資料链接:https://pan.baidu.com/s/1ccF2xEVgRRKojIHbdKwNLA
      提取码:r6f6

    • 大家好
      最近在嘗試非線性回歸

      require(ggplot2)
      warnings('off')
      x <- rep(c(1, 2, 3, 5, 7, 10),each=4)
      y <-c( 105,
        97,
        104,
        106,
        136,
        161,
        151,
        153,
        173,
        179,
        174,
        174,
        195,
        182,
        201,
        172,
        207,
        194,
        206,
        213,
        218,
        193,
        235,
        229
      )
      ds <- data.frame(day = x, data = y)
      
      model <- nls(
        # y ~ b0 * (1- exp(-b1 * x)),
        y ~ b0 * x / (b1 + x),
        data = ds,
        start = list(b0 = 200,
                     b1 = 2),
        trace = T
      )

      雖然以上程式可以執行,但卻會跑出以下警告

      Warning messages:
      1: In min(x) : min 中沒有無漏失的引數; 回傳 Inf
      2: In max(x) : max 中沒有無漏失的引數;回傳 -Inf

      這個警告訊息應該是在設定nls起始值的時候沒有設定的很好的關係?加了warnings('off')也沒用

      最後在knitr成pdf的時候會失敗

      想知道這樣的狀況有沒有可以knit的方法?

      附上yaml跟header檔
      感謝大家

      title: "Nonlinear"
      author: You Jia Sun
      date: "最近一次編輯時間 `r format(Sys.time(), '%Y年%m月%d日')`"
      geometry: margin=1.18in
      documentclass: ctexart
      output: 
        bookdown::pdf_book: 
          number_sections: yes
          pandoc_args: --listings
          toc: yes
          fig_crop: no
          base_format: rmarkdown::pdf_document
          latex_engine: xelatex
          citation_package: natbib
          dev: "cairo_pdf"
          template: null
          extra_dependencies:
            sourcecodepro:
             - scale=0.85
            sourceserifpro:
             - rmdefault
            sourcesanspro:
             - sfdefault
            awesomebox:
          quote_footer: ["\\hspace*{\\fill} ", ""]
          geometry:
            - top=30mm
            - left=20mm
          includes:
            in_header: header.tex
      biblio-style: plainnat #apalike
      natbiboptions: "authoryear,round"
      link-citations: yes
      colorlinks: yes
      classoption: "UTF8,a4paper,fontset=adobe,zihao=false"
      \usepackage{xeCJK}
      \usepackage{listings}
      \setCJKmainfont{微軟正黑體}
      \lstset{
        breaklines=true
      }
    • 大家好,目前有月資料

      Time,Mkt_RF,SMB,HML,RF
      192607,2.96,-2.3,-2.87,0.22
      192608,2.64,-1.4,4.19,0.25
      192609,0.36,-1.32,0.01,0.23
      192610,-3.24,0.04,0.51,0.32
      192611,2.53,-0.2,-0.35,0.31

      因為想對日期作篩選出192608192610的資料,所以用as.Dateas.POSIXct去轉換,可是卻發現轉出來的都是NA

      自己測試了一下發現這兩個指令好像都只能轉換有%d的日期,而且%y%m%d中間一定要有-連接

      > as.Date("192702",format="%Y-%m")
      [1] NA
      > as.Date("19270211",format="%Y-%m-%d")
      [1] NA
      > as.Date("1927-02-11",format="%Y-%m-%d")
      [1] "1927-02-11"
      > as.POSIXct("1927-02-11",format="%Y-%m-%d")
      [1] "1927-02-11 CST"
      > as.POSIXct("1927-02",format="%Y-%m")
      [1] NA
      > as.POSIXct("192702",format="%Y-%m")
      [1] NA

      如果真的是這樣的話,目前資料沒有到%d,中間也沒有-連接,該如何轉換才好?

      • Liechi 對 因為原數據太多所以才只擷取一段 下次會注意 感謝大家的包容

      • Liechi 恭喜你,難道是我電腦問題嗎

        R version 4.0.2 (2020-06-22)
        Platform: x86_64-apple-darwin17.0 (64-bit)
        Running under: macOS Catalina 10.15.7
        
        Matrix products: default
        BLAS:   /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
        LAPACK: /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRlapack.dylib
        
        locale:
        [1] zh_TW.UTF-8/zh_TW.UTF-8/zh_TW.UTF-8/C/zh_TW.UTF-8/zh_TW.UTF-8
        
        attached base packages:
        [1] stats     graphics  grDevices utils     datasets  methods   base     
        
        loaded via a namespace (and not attached):
        [1] compiler_4.0.2 tools_4.0.2 
        • dapengde
          換用header = TRUE後也是一樣的結果。
          程式剛開始寫,還沒賦值,只有一些rmarddown的設定而已

          R version 4.0.2 (2020-06-22)
          Platform: x86_64-apple-darwin17.0 (64-bit)
          Running under: macOS Catalina 10.15.7
          
          Matrix products: default
          BLAS:   /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
          LAPACK: /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRlapack.dylib
          
          locale:
          [1] zh_TW.UTF-8/zh_TW.UTF-8/zh_TW.UTF-8/C/zh_TW.UTF-8/zh_TW.UTF-8
          
          attached base packages:
          [1] stats     graphics  grDevices utils     datasets  methods   base     
          
          loaded via a namespace (and not attached):
          [1] compiler_4.0.2 tools_4.0.2  
        • tctcab

          了解,奇怪的是我把名稱補回去源数据後,還是得到一樣的錯誤

          Time,Mkt_RF,SMB,HML,RF
          192607,2.96,-2.3,-2.87,0.22
          192608,2.64,-1.4,4.19,0.25
          192609,0.36,-1.32,0.01,0.23
          192610,-3.24,0.04,0.51,0.32
          192611,2.53,-0.2,-0.35,0.31
          data <-
            read.csv(
              "C:/Users/Yoga/Desktop/F-F_Research_Data_Factors.csv",
              header = T,
              colClasses = c("character", rep("numeric", 4))
            )
          Error in scan(file = file, what = what, sep = sep, quote = quote, dec = dec, : scan() 需要 'a real',而不是 'Mkt-RF'

          好崩潰阿,這到底是怎麼回事

          • yihui 抱歉 我沒有想到CSV 数据、错误消息也是文本,已經修改好了

          • 大家好,以下是資料,可以看到時間欄位沒有名稱

            Mkt_RF,SMB,HML,RF
            192607,2.96,-2.3,-2.87,0.22
            192608,2.64,-1.4,4.19,0.25
            192609,0.36,-1.32,0.01,0.23
            192610,-3.24,0.04,0.51,0.32
            192611,2.53,-0.2,-0.35,0.31
            192612,2.62,-0.04,-0.02,0.28

            因為發現直接讀取會全部都變成文字,所以加上colClasses 指定讀取型態,但卻出現錯誤

            data <-
              read.csv(
                "C:/Users/Yoga/Desktop/F-F_Research_Data_Factors.csv",
                header = T,
                colClasses = c("character", rep("numeric", 4))
              )
            summary(data)
            Error in scan(file = file, what = what, sep = sep, quote = quote, dec = dec, : scan() 需要 'a real',而不是 'Mkt-RF'

            因為是缺少欄位名稱,所以也有試過給定欄位名稱,可是結果也一樣

            data <-
              read.csv(
                "C:/Users/Yoga/Desktop/F-F_Research_Data_Factors.csv",
                header = T,col.names=c("time", "Mkt.RF", "SMB","HML","RF"),
                colClasses = c("character", rep("numeric", 4))
              )

            請問是否有人知道改怎麼做,感謝

            R version 4.0.2 (2020-06-22)
            Platform: x86_64-apple-darwin17.0 (64-bit)
            Running under: macOS Catalina 10.15.7
            
            Matrix products: default
            BLAS:   /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
            LAPACK: /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRlapack.dylib
            
            locale:
            [1] zh_TW.UTF-8/zh_TW.UTF-8/zh_TW.UTF-8/C/zh_TW.UTF-8/zh_TW.UTF-8
            
            attached base packages:
            [1] stats     graphics  grDevices utils     datasets  methods   base     
            
            loaded via a namespace (and not attached):
            [1] compiler_4.0.2 tools_4.0.2 
            • 我透過tex檔案可以更改pdf內文的中文字體(綠色圈),但發現"目錄"這兩個字的字體都不會變(紅色圈),請問有什麼方法可以改嗎?

              附上yaml

              title: "Statistical Methods in Finance HW3"
              author: You Jia Sun
              date: "最近一次編輯時間 `r format(Sys.time(), '%Y年%m月%d日%H时%M分%S秒')`"
              geometry: margin=1.18in
              documentclass: ctexart
              output: 
                bookdown::pdf_book: 
                  number_sections: yes
                  pandoc_args: --listings
                  toc: yes
                  fig_crop: no
                  base_format: rmarkdown::pdf_document
                  latex_engine: xelatex
                  citation_package: natbib
                  dev: "cairo_pdf"
                  template: null
                  extra_dependencies:
                    sourcecodepro:
                     - scale=0.85
                    sourceserifpro:
                     - rmdefault
                    sourcesanspro:
                     - sfdefault
                    awesomebox:
                  quote_footer: ["\\hspace*{\\fill} ", ""]
                  geometry:
                    - top=30mm
                    - left=20mm
                  includes:
                    in_header: header.tex
              biblio-style: plainnat
              natbiboptions: "authoryear,round"
              link-citations: yes
              colorlinks: yes
              classoption: "UTF8,a4paper,fontset=adobe,zihao=false"

              附上tex檔

              \usepackage{xeCJK}
              \usepackage{listings}
              \setCJKmainfont{微軟正黑體}
              \lstset{
                breaklines=true
              }
              > sessionInfo()
              R version 4.0.2 (2020-06-22)
              Platform: x86_64-w64-mingw32/x64 (64-bit)
              Running under: Windows 10 x64 (build 19041)
              
              Matrix products: default
              
              locale:
              [1] LC_COLLATE=Chinese (Traditional)_Taiwan.950  LC_CTYPE=Chinese (Traditional)_Taiwan.950   
              [3] LC_MONETARY=Chinese (Traditional)_Taiwan.950 LC_NUMERIC=C                                
              [5] LC_TIME=Chinese (Traditional)_Taiwan.950    
              
              attached base packages:
              [1] stats     graphics  grDevices utils     datasets  methods   base     
              
              loaded via a namespace (and not attached):
               [1] compiler_4.0.2  htmltools_0.5.0 tools_4.0.2     yaml_2.2.1      rmarkdown_2.4  
               [6] knitr_1.30      xfun_0.18       digest_0.6.25   rlang_0.4.8     evaluate_0.14
            • Fye 感謝回覆

              tinytex::tlmgr_install("listings")執行會出現錯誤

              檢查發現我的mitex裡面有listings宏包了,可能是這個原因?

              不過加上pandoc_args: --listings可以順利執行了,現在的新問題是執行完後沒有效果

              附上我的yaml跟tex檔,是不是我有指令設錯了呢?

              title: "Statistical Methods"
              author: You Jia Sun
              date: "最近一次編輯時間 `r format(Sys.time(), '%Y年%m月%d日%H时%M分%S秒')`"
              geometry: margin=1.18in
              documentclass: ctexart
              output: 
                bookdown::pdf_book: 
                  number_sections: yes
                  toc: yes
                  fig_crop: no
                  base_format: rmarkdown::pdf_document
                  latex_engine: xelatex
                  citation_package: natbib
                  dev: "cairo_pdf"
                  template: null
                  extra_dependencies:
                    sourcecodepro:
                     - scale=0.85
                    sourceserifpro:
                     - rmdefault
                    sourcesanspro:
                     - sfdefault
                    awesomebox:
                  quote_footer: ["\\hspace*{\\fill} ", ""]
                  geometry:
                    - top=30mm
                    - left=20mm
                  includes:
                    in_header: header.tex
              biblio-style: plainnat
              pandoc_args: --listings
              natbiboptions: "authoryear,round"
              link-citations: yes
              colorlinks: yes
              classoption: "UTF8,a4paper,fontset=adobe,zihao=false"
              \usepackage{xeCJK}
              \usepackage{listings}
              \setCJKmainfont{微軟正黑體}
              \lstset{
                breaklines=true
              }

              感謝

              • 大家好

                我的knitr可以順利執行,但是編譯出來的pdf都沒有顯示中文

                文檔設置、程式和結果在此
                https://imgur.com/a/TGyewwj

                不知道是否有人遇過這樣的狀況?