2017-11-01 21 views
0

3Dプロットで2つの平行平面をプロットパッケージで視覚化しようとしています(Datacampにアクセスできる人はこの練習です:https://campus.datacamp.com/courses/multiple-and-logistic-regression/multiple-regression?ex=9プロットパッケージで複数の平行平面を3Dプロットに追加する(datacampエクササイズ)

このグラフの作成時点では、カテゴリ変数に一致するプレーンに合わせて行列を作成する必要があります(plane_new =条件はnew、plane_used = conditionが使用されます)。問題は、私が入力として使用した2つのデータセット(2つのレベルの状態に対応する)は、観測数が異なることです。私はこれらの行列をどのように匹敵するようにすることができないのか分からないように思えます。そのため、飛行機は図の幾何学的オブジェクトとして正しくフィットします。

Rウィザードが私を助けてくれることを願っています。

# libraries 
library(openintro) #exemplary datasets 
library(modelr) #multivariate methods 
library(broom) #tidy 
library(ggplot2) #visualizing data 
library(plotly) #visualizing models in 3D 

## Fit the model 

lm_ext <- lm(totalPr ~ duration + startPr + cond,   # Interpretation: With every 1 unit increase of auction duration (unit = day), the price of the game decreases with .51 units in the response variable (total price), when keeping startPr constant. The eventual value of the predicted value also depends on condition (categorical), for which the y-intercept is different 
      data = marioKart)   


## Visualize the model (including predictions) 

marioKart_ss_new <- subset(marioKart, cond == "new")  # To visualize planes in a 3D graph in plotly, the dataframe needs to be split in the number of levels of the categorical variable 
marioKart_ss_used <- subset(marioKart, cond == "used") 

duration_new <- as.vector(marioKart_ss_new$duration)  # These vectors represent the linear model for condition = new 
startPr_new <- as.vector(marioKart_ss_new$startPr) 

duration_used <- as.vector(marioKart_ss_used$duration)  # These vectors represent the linear model for condition = used 
startPr_used <- as.vector(marioKart_ss_used$startPr) 

lm_new <- lm(totalPr ~ duration + startPr,     # Create two linear models 
      data = marioKart_ss_new) 
lm_used <- lm(totalPr ~ duration + startPr, 
      data = marioKart_ss_used) 

grid_new <- marioKart_ss_new %>%       # Make two grids with all combinations of the levels of the two numerical explanatory variables 
       data_grid(duration = 
          seq_range(duration, by = 1), 
         startPr = 
          seq_range(startPr, by = 1)) 
grid_used <- marioKart_ss_used %>% 
       data_grid(duration = 
          seq_range(duration, by = 1), 
         startPr = 
          seq_range(startPr, by = 1)) 

lm_new <- lm(totalPr ~ duration + startPr,     # Make two seperate models based on the two levels of the categorical explanatory variable 
      data = marioKart_ss_new) 
lm_used <- lm(totalPr ~ duration + startPr,     
      data = marioKart_ss_used) 

pred_new <- augment(lm_new, newdata = grid_new)    # Predictions 
pred_used <- augment(lm_used, newdata = grid_used) 

plane_new <- matrix(pred_new$.fitted,      # Matrix of preditions as input for planes 
        nrow = 70, 
        ncol = 70) 

plane_used <- matrix(pred_used$.fitted,      
        nrow = 55, 
        ncol = 55) 

plot <- plot_ly(data = marioKart,       # 3D plot of datapoints 
       z = ~totalPr, 
       x = ~duration, 
       y = ~startPr, 
       opacity = 0.6) %>% 
    add_markers(color = ~cond) 

plot %>%             # Add planes 
    add_surface(x = ~duration_new,       ### NOT WORKING, WAIT FOR DATACAMP 
       y = ~startPr_new, 
       z = ~plane_new, 
       showscale = FALSE) %>% 
    add_surface(x = ~duration_used, 
       y = ~duration_used, 
       z = ~plane_used, 
       showscale = FALSE) 

答えて

0

ないコードウィザードここではなく、同じことを尋ね:ここに私のコードは

library(tidyverse) 
library(modelr) 

grid <- mario_kart %>% 
    modelr::data_grid(
    duration = seq_range(duration, n = 70), 
    startPr = seq_range(startPr, n = 70), 
    cond 
) 

library(broom) 

tidy_planes <- mod %>% 
    augment(newdata = grid) 

x <- unique(grid$duration) 
y <- unique(grid$startPr) 

plane0 <- tidy_planes %>% 
    filter(cond == "new") %>% 
    pull(.fitted) %>% 
    matrix(nrow = length(x), byrow = TRUE) 

plane1 <- tidy_planes %>% 
    filter(cond == "used") %>% 
    pull(.fitted) %>% 
    matrix(nrow = length(x), byrow = TRUE) 
+0

うん、それはそれです!ベンジャミンありがとう – SHW

関連する問題