|
| 1 | +--- |
| 2 | +title: "Week 9 Class" |
| 3 | +toc: false |
| 4 | +--- |
| 5 | + |
| 6 | + |
| 7 | +# Last Class Review |
| 8 | +```js |
| 9 | +// const stocks = await FileAttachment("./stock_data/stocks.csv").csv({ typed: true }) |
| 10 | +// const events = await FileAttachment("./stock_data/stock_events.csv").csv({ typed: true }) |
| 11 | +// display(stocks.slice(0, 10)) |
| 12 | +// display(events) |
| 13 | +``` |
| 14 | + |
| 15 | +<!-- Create an array of unique tickers: --> |
| 16 | +```js |
| 17 | +// const allTickers = stocks.map(d => d.Ticker) |
| 18 | +// // display(allTickers) |
| 19 | +// const setTickers = new Set(allTickers) |
| 20 | +// const uniqueTickers = Array.from(setTickers) |
| 21 | +// display(uniqueTickers) |
| 22 | +``` |
| 23 | + |
| 24 | +<!-- ```js |
| 25 | +const selectedStock = view(Inputs.select( |
| 26 | + [null, ...uniqueTickers], |
| 27 | + { value: "AAPL" } //"select" } |
| 28 | + )) |
| 29 | +``` --> |
| 30 | + |
| 31 | +<!-- Selected stock is: ${selectedStock} --> |
| 32 | + |
| 33 | +<!-- ```js |
| 34 | +const filteredStocks = stocks.filter(d => d.Ticker === selectedStock) |
| 35 | +const filteredEvents = events.filter(d => d["Related Tickers"].includes(selectedStock)) |
| 36 | +display(filteredEvents) |
| 37 | +``` --> |
| 38 | + |
| 39 | +<!-- ```js |
| 40 | +display(width) |
| 41 | +``` --> |
| 42 | + |
| 43 | +<!-- ```js |
| 44 | +Plot.plot({ |
| 45 | + title: selectedStock === null |
| 46 | + ? "pick a stock to continue" |
| 47 | + : `Viewing: ${selectedStock}`, |
| 48 | + height: 200, |
| 49 | + width, |
| 50 | + marks: [ |
| 51 | + Plot.line( |
| 52 | + // stocks, |
| 53 | + filteredStocks, |
| 54 | + { |
| 55 | + x: "Date", |
| 56 | + y: "Close", |
| 57 | + z: "Ticker", |
| 58 | + // Changing transparency |
| 59 | + stroke: d => d["Ticker"] === selectedStock ? "black" : "none", |
| 60 | + }), |
| 61 | + Plot.linearRegressionY(filteredStocks, {x: "Date", y: "Close", stroke: "red"}), |
| 62 | + Plot.ruleY([0]), |
| 63 | + Plot.ruleX([new Date(2018, 0, 1)]), |
| 64 | + Plot.dot( |
| 65 | + filteredEvents, |
| 66 | + { |
| 67 | + x: "Date", |
| 68 | + y: 0, |
| 69 | + tip: true, |
| 70 | + channels: { |
| 71 | + "Event": "Event Name", |
| 72 | + "Notes": "Notes" |
| 73 | + } |
| 74 | + } |
| 75 | + ), |
| 76 | + Plot.tip([filteredEvents[11]], { |
| 77 | + x: "Date", //filterEvents.date |
| 78 | + // x: filteredEvents[0].Date |
| 79 | + channels: { |
| 80 | + "Event": "Event Name", |
| 81 | + "Notes": "Notes" |
| 82 | + } |
| 83 | + }) |
| 84 | + ] |
| 85 | +}) |
| 86 | +``` --> |
| 87 | +<!-- |
| 88 | +```js |
| 89 | +Plot.plot({ |
| 90 | + title: selectedStock === null |
| 91 | + ? "pick a stock to continue" |
| 92 | + : `Viewing: ${selectedStock}`, |
| 93 | + height: 200, |
| 94 | + width, |
| 95 | + marks: [ |
| 96 | + Plot.line( |
| 97 | + // stocks, |
| 98 | + filteredStocks, |
| 99 | + { |
| 100 | + x: "Date", |
| 101 | + y: "Close", |
| 102 | + z: "Ticker", |
| 103 | + }), |
| 104 | + Plot.ruleY([0]), |
| 105 | + Plot.dot( |
| 106 | + filteredEvents, |
| 107 | + { |
| 108 | + x: d => d["Date"], |
| 109 | + y: event => { |
| 110 | + const thisStockObj = filteredStocks.filter(stock => |
| 111 | + stock.Date.toDateString() === event.Date.toDateString() |
| 112 | + // && event["Related Tickers"].includes(stock.Ticker) |
| 113 | + // && stock.Ticker === selectedStock |
| 114 | + ) |
| 115 | + console.log(thisStockObj) |
| 116 | + // return 0 |
| 117 | + return thisStockObj[0]?.Close || 0 |
| 118 | + }, |
| 119 | + tip: true, |
| 120 | + channels: { |
| 121 | + "Event": "Event Name", |
| 122 | + "Notes": "Notes" |
| 123 | + } |
| 124 | + } |
| 125 | + ), |
| 126 | + ] |
| 127 | +}) |
| 128 | +``` --> |
| 129 | + |
| 130 | + |
| 131 | + |
| 132 | +```js |
| 133 | +const viewership = await FileAttachment('./viewership_data/viewership.csv').csv({ typed: true }) |
| 134 | +const cost = await FileAttachment('./viewership_data/production_cost.csv').csv({ typed: true }) |
| 135 | + |
| 136 | +display(viewership) |
| 137 | +display(cost) |
| 138 | +``` |
| 139 | + |
| 140 | +```js |
| 141 | +view(Inputs.table(viewership)) |
| 142 | +``` |
| 143 | + |
| 144 | + |
| 145 | +<!-- ```js |
| 146 | +Plot.plot({ |
| 147 | + width, |
| 148 | + marginLeft: 120, |
| 149 | + marks: [ |
| 150 | + Plot.barX(viewership, |
| 151 | + { |
| 152 | + x: "total_estimated_viewership", |
| 153 | + y: "show_name", |
| 154 | + sort: { y: "-x" } |
| 155 | + } |
| 156 | + ) |
| 157 | + ] |
| 158 | +}) |
| 159 | +``` |
| 160 | +
|
| 161 | +
|
| 162 | +```js |
| 163 | +Plot.plot({ |
| 164 | + width, |
| 165 | + marginLeft: 120, |
| 166 | + marks: [ |
| 167 | + Plot.barX(cost, |
| 168 | + { |
| 169 | + x: "total_production_cost_usd", |
| 170 | + y: "show_name", |
| 171 | + sort: { y: "-x" } |
| 172 | + } |
| 173 | + ) |
| 174 | + ] |
| 175 | +}) |
| 176 | +``` --> |
| 177 | + |
| 178 | +<!-- ```js |
| 179 | +const newData = cost.map(d => ({ ...d, newValue: "E"})) |
| 180 | +view(Inputs.table(newData)) |
| 181 | +``` --> |
| 182 | + |
| 183 | + |
| 184 | + |
| 185 | +```js |
| 186 | +Plot.plot({ |
| 187 | + width, |
| 188 | + marginLeft: 120, |
| 189 | + x: { |
| 190 | + tickFormat: tick => `${tick} vpd` |
| 191 | + }, |
| 192 | + color: { |
| 193 | + scheme: "YlOrRd", |
| 194 | + legend: true, |
| 195 | + tickFormat: tick => d3.format(".1s")(tick) |
| 196 | + }, |
| 197 | + marks: [ |
| 198 | + Plot.barX(viewership, // always look here |
| 199 | + { |
| 200 | + y: d => d["show_name"], |
| 201 | + fill: "total_estimated_viewership", |
| 202 | + x: (views) => { |
| 203 | + // console.log("show viewers:", views) |
| 204 | + const thisShowCost = cost.find(c => { |
| 205 | + // console.log("c:", c) |
| 206 | + // console.log("check:", |
| 207 | + // c.show_name, |
| 208 | + // views.show_name, |
| 209 | + // c.show_name === views.show_name) |
| 210 | + return c.show_name === views.show_name |
| 211 | + }) |
| 212 | + // console.log("thisShowCost", thisShowCost) |
| 213 | + const newMetric = views.total_estimated_viewership / thisShowCost.total_production_cost_usd |
| 214 | + return newMetric |
| 215 | + }, |
| 216 | + sort: { y: "-x" } |
| 217 | + } |
| 218 | + ) |
| 219 | + ] |
| 220 | +}) |
| 221 | +``` |
| 222 | + |
| 223 | + |
| 224 | +# AI HELP |
| 225 | + |
| 226 | +Here's what I asked AI: |
| 227 | +<div style="font-family: monospace; font-size: 12px; background: lightgrey; border-radius: 2px; padding: 15px 10px"> |
| 228 | +I am working with Observable Plot, and I would like to join two datasets in this file (viewership, costs) on show name. I want to make a new metric, TOTAL views per TOTAL dollar (spent). |
| 229 | + |
| 230 | +Here is the cost data structure: |
| 231 | +{ |
| 232 | + show_name: "Stranger Things" |
| 233 | + cost_per_episode_usd: 12000000 |
| 234 | + number_of_episodes: 42 |
| 235 | + total_production_cost_usd: 504000000 |
| 236 | +} |
| 237 | + |
| 238 | +Here is the views data structure: |
| 239 | +{ |
| 240 | + show_name: "Stranger Things" |
| 241 | + avg_weekly_viewers: 15200000 |
| 242 | + launch_date: 2016-07-15 |
| 243 | + weeks_since_launch: 486 |
| 244 | + total_estimated_viewership: 7387200000 |
| 245 | +} |
| 246 | +</div> |
| 247 | + |
| 248 | +It returned: |
| 249 | +```js |
| 250 | +// Join the datasets and create the new metric |
| 251 | +const joinedData = viewership.map(show => { |
| 252 | + // Find the matching cost data for this show |
| 253 | + const matchingCost = cost.find(c => c.show_name === show.show_name); |
| 254 | + |
| 255 | + if (matchingCost) { |
| 256 | + return { |
| 257 | + show_name: show.show_name, |
| 258 | + total_estimated_viewership: show.total_estimated_viewership, |
| 259 | + total_production_cost_usd: matchingCost.total_production_cost_usd, |
| 260 | + views_per_dollar: show.total_estimated_viewership / matchingCost.total_production_cost_usd |
| 261 | + }; |
| 262 | + } |
| 263 | + return null; // If no matching cost data found |
| 264 | +}).filter(d => d !== null); // Remove any shows without cost data |
| 265 | + |
| 266 | +display(joinedData); |
| 267 | +``` |
| 268 | +<!-- selectedStock: ${select} --> |
| 269 | + |
| 270 | +<div class="card"> |
| 271 | +${Plot.plot({ |
| 272 | + width, |
| 273 | + marginLeft: 120, |
| 274 | + title: "Views per Dollar Spent", |
| 275 | + marks: [ |
| 276 | + Plot.barX(joinedData, { |
| 277 | + x: "views_per_dollar", |
| 278 | + y: "show_name", |
| 279 | + sort: { y: "-x" }, |
| 280 | + tip: true |
| 281 | + }) |
| 282 | + ] |
| 283 | +})} |
| 284 | +</div> |
0 commit comments