開始使用免費開始

檢視類別型輸入的結構

在這個練習中,你會呼叫 model.matrix()文件)來 觀察 R 在建模時如何同時表示類別型與數值型的輸入。 資料集 flowers(源自 Sleuth3 套件)已為你載入。它包含下列欄位:

  • Flowersmeadowfoam 植物的平均花朵數
  • Intensity:施加在植物上的光照處理強度
  • Time:類別變數——光照處理在生命週期中的時間點(LateEarly

最終目標是將 Flowers 作為 TimeIntensity 的函數來進行預測。

本練習屬於課程

R 中的監督式學習:回歸

檢視課程

練習說明

  • flowers 呼叫 str() 函式,查看各欄位的型別。
  • 對欄位 flowers$Time 使用 unique(),查看 Time 可能的取值。有多少個不重複的值?
  • 建立一個 formula,將 Flowers 表示為 IntensityTime 的函數。指定給變數 fmla,並列印它。
  • 使用 fmlamodel.matrix(),為資料框 flowers 建立模型矩陣。指定給變數 mmat
  • 使用 head() 檢視 flowers 的前 20 列。
  • 接著檢視 mmat 的前 20 列。
    • 數值欄位 Intensity 有不同嗎?
    • 來自 flowers 的類別欄位 Time 發生了什麼變化?
    • Time == 'Early' 如何被表示?那 Time == 'Late' 呢?

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# Call str on flowers to see the types of each column
___

# Use unique() to see how many possible values Time takes
___

# Build and print a formula to express Flowers as a function of Intensity and Time: fmla
(fmla <- ___("Flowers ~ Intensity + Time"))

# Use fmla and model.matrix to see how the data is represented for modeling
mmat <- ___

# Examine the first 20 lines of flowers
___

# Examine the first 20 lines of mmat
___
編輯並執行程式碼